Dynamic GUI
Hello people...

really I dont know if this is the correct name (Dynamic GUI)
but I wanna ask u how to build dynamic JButtons ..
4 example :
the number of the buttons in a frame not static ...
(user enter it) at the begging and the buttons appers on the frame in the
runtime mode ... may I have to define arraylist of buttons (in JbInt() ) ..
but how and where should I make it generates (ancestorAdapter & actionListner )4 the butons ??
plz help me if someone have an example code 4 this applecation
and can I use the same environment of a java applecation->Jframe 4 this project or it has special tools ?
sorry I'm beginner in JAVA but I hope I could explain my idea well ..
and if u didnt understand something plz tell me ,,and I will try to write it again in other words...

Recommended Answers

All 4 Replies

Your question is a little vague,
I guess you want a button to generate then open a JFrame.
I can't answer your question specifically, but perhaps the following will help.
1. Use arrays or LinkedLists of buttons, so you can loop through them,
you don't really want to write code for each case(eg, n=3 buttons, n=6 buttons).

2. You need to choose a layout to deal with your gui.
You are probably best to get a JPanel, then set its layout to what you want.
I like to use null layout a lot, for each component you add to frame, you should:
a) set its size.
b)set its location
Then just add it to frame.
If you are careful with the maths, you can get 3 buttons spaced/sized/positioned anyway you want, or 5 buttons or 10 buttons.

Can't think of anything else for now, perhaps if you can be more specific with the details, I can give you more info.

Thanx alot cms271828 4 helpin me ...
this is exactly what I was asking for ...I tried what u suggest (by defining an Array of buttons)and It works successfully

but there's something else :

my idea is to display a JtextField at the begining to enter (n:the number of the buttons user wants to be on the frame)
I excute this step and it works by defining an Array of buttons and build it in the constructor of the frame...
but I wanna make an option to allow user to add a new button to this array
u know array cant be dynamic (I cant add new element to it)so it needs ArrayList or LinkedList of buttons not an Array ..right ??

but I dont know how to deal with an ArrayList of buttons
(I couldnt add its elements to the contentPane)...

I tried this :

private ArrayList A=new ArrayList();
A.add(button,i); //I've set button's(bounds,text,color) before this step
contentPane.add(A.get(i));// this is the problem,,compiler gives error here

so wolud u plz write the right code to declare waht is the error here??

I think your parameters for ArrayList.add(..)
are back to front.
The index comes first.
I've never really used ArrayList to be honest, I like LinkedList, but they are pretty similar.

So you would just loop through the elements..
for(int lp=0;lp<list.size;lp++)
{
getContentPane().add((JButton)(list.get(lp)));
}

You can ignore that cast if you are using Java 5.0 and taking advantage of generics.

Thanx so much 4 ur help & sorry 4 takin ur time ...
I tried ur suggestion and every thing is ok now ...
Regards,,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.