File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes vectors and gridpanels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "vectors and gridpanels" Watch "vectors and gridpanels" New topic

vectors and gridpanels


Joined: Nov 16, 2003
Posts: 1
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");
b2 = new JButton("2");
b3 = new JButton("3");
ButtonHandler handler = new ButtonHandler();
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
any ideas? i really need to do this using a vector so only vector solutions please,
Many Thanks
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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.
// or even this which i was sure would work
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.
I agree. Here's the link:
subject: vectors and gridpanels
It's not a secret anymore!