Jump to content
xisto Community
wutske

Create Dynamic Gui ?

Recommended Posts

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 :rolleyes: .My first tought was to create an array of checkboxes, but this doesn't seem to work B) .Any ideas ?

Share this post


Link to post
Share on other sites

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

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

I think the cold classroom was freezing my brains a bit :rolleyes: . 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 B)

@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: I know vectors are more dynamic then arrays, but are there any other advantagen when using them ?

Lots. :rolleyes:

They're thread safe

They're easier to work with

Parameterized, there's no casting needed like there was in 1.4-

Serialize easier

Implement most of the collections interfaces, so you get sorting, iterating, serialization...right out of the box

Built in batch methods for adding collections to collections

Object 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 by ethergeek (see edit history)

Share this post


Link to post
Share on other sites

Allright, crap :o , I still have a lot of things to learn about java :rolleyes: , EDT, serlialization B) ... 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 :P

Edited by wutske (see edit history)

Share this post


Link to post
Share on other sites

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

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 :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.