wutske 0 Report post Posted November 5, 2007 I'm currently working on a small project for school. I need to place a variable amount of checkboxes in a jPanel, but I have no idea how to start .My first tought was to create an array of checkboxes, but this doesn't seem to work .Any ideas ? Share this post Link to post Share on other sites
ethergeek 0 Report post Posted November 5, 2007 You can stick them in an array to keep track of them (though if it's variable, I would use Vector<JCheckBox> to store them. Then just iterate across the array to add them to the form. Share this post Link to post Share on other sites
.:Brian:. 0 Report post Posted November 5, 2007 Can't you use an arrayList or something to store checkbox objects (and add them and such)?I am not sure as I haven't ever done any GUI programming in java, so I don't know too much about it, but I would assume you could use an arrayList to store the objects and keep track of them somehow? Share this post Link to post Share on other sites
wutske 0 Report post Posted November 5, 2007 I think the cold classroom was freezing my brains a bit . It is possible to create an array of checkboxes and I did it this way (it's still very basic): private void generateComponents() { JCheckBox[] seat_checks; seat_checks = new JCheckBox[6]; for (int i=0;i<6;i++) { seat_checks[i] = new JCheckBox("seat " + String.valueOf(i)); checkspanel.add(seat_checks[i]); } } Still don't know why it didn't work this morning @ethergeek: I know vectors are more dynamic then arrays, but are there any other advantagen when using them ? Share this post Link to post Share on other sites
ethergeek 0 Report post Posted November 5, 2007 (edited) @ethergeek: I know vectors are more dynamic then arrays, but are there any other advantagen when using them ?Lots. They're thread safeThey're easier to work withParameterized, there's no casting needed like there was in 1.4-Serialize easierImplement most of the collections interfaces, so you get sorting, iterating, serialization...right out of the boxBuilt in batch methods for adding collections to collectionsObject oriented collections almost always have an advantage over simple collections like arrays. The notable exception here is arrays of primitives...you can't make collections of primitives, so the JVM has to autobox/unbox them when you try...this can be slow if done in a large loop. Edit: I forgot to mention: when updating a GUI on the fly, it's a good idea to pass it off to the Event Dispatch Thread (EDT) so as to avoid painting inconsistencies. This is easily done using the SwingUtilities.invokeAndWait() method. Edited November 5, 2007 by ethergeek (see edit history) Share this post Link to post Share on other sites
wutske 0 Report post Posted November 7, 2007 (edited) Allright, crap , I still have a lot of things to learn about java , EDT, serlialization ... I think I'll spend my weekend reading, reading and more reading :P//edit: as soon as I've found out how to use vector, I'll implement them in my program , don't want to write a crappy project, going for at least 18/20 Edited November 7, 2007 by wutske (see edit history) Share this post Link to post Share on other sites
Moo64c 0 Report post Posted May 30, 2008 I have no idea what is the vector thing, but I'd go for an ArrayList<JCheckBox>. It's as dynamic as you'd need.for example:Adding a checkbox will result in:1. extending the gui screen a bit more2. adding the checkbox to the ArrayList3. adding the checkbox to the gui. etc. Share this post Link to post Share on other sites
wutske 0 Report post Posted May 30, 2008 Moo64c, this topic is almost one year old, I've turned the application in a long time ago .Also, watch out for bumping old topics, the mods don't like it if you do it too often Share this post Link to post Share on other sites