Hi all, this is my first ever post on java ranch, My problem is this, have an applet that uses the grid layout manager, i have a program that adds 10 buttons to an applet, like a calculator, but i want to put them in a vector any ideas how to do it, this is a sample of some working code i have: b1 = new JButton("1"); add(b1); b2 = new JButton("2"); add(b2); b3 = new JButton("3"); add(b3); ButtonHandler handler = new ButtonHandler(); b1.addActionListener(handler); b2.addActionListener(handler); b3.addActionListener(handler); and this is what i am trying to do, but it doest like it when i try to add the vector to the gridpanel Vector vButtons = new Vector(); vButtons.addElement (b1); vButtons.addElement (b2); vButtons.addElement (b3); add(vButtons); // doesnt like either this statement or the next // or even this which i was sure would work add(vButtons.elementAt(4)); any ideas? i really need to do this using a vector so only vector solutions please, Many Thanks Tim
Tim(uni studnt needs a job, frm uk): Welcome to JavaRanch! We don't have many rules here at JavaRanch, but we do have one. Please change your display name to comply with The JavaRanch Naming Policy. Also, when posting code, please use the [CODE] and [/CODE] tags -- it makes it easier to read. Thanks Pardner! To answer your question, a Vector is not a displayable object, and it does not make sense to add it to a container. What you can do is create a Vector of buttons and then iterate through the Vector, placing each Button in the container. [code] // or even this which i was sure would work add(vButtons.elementAt(4)); [/code] This is the right approach, but you need to cast the element returned from the Vector to a Button (or, more generically, a Component) before adding it.
And I will highly recommend using java.util.Lists instead of Vectors (since Vectors implement the List interface, you can still use Vectors as your underlying obejcts if you want...) Of course, if you are being regulated by your professor/boss, then of course do what they tell you (although I would recommend trying to talk them out of any insistance on Vectors that they might have...) [ November 16, 2003: Message edited by: Joel McNary ]
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.