File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Adding components dynamically to a nested JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding components dynamically to a nested JPanel" Watch "Adding components dynamically to a nested JPanel" New topic
Author

Adding components dynamically to a nested JPanel

Harihara Sudan
Greenhorn

Joined: Sep 27, 2011
Posts: 2
Hi,

I am a new Java Programmer.could you help me in resolving this.

I am trying to created a JPanel withing a main JPanel. For the nested one, i have included Grid Layout and i am trying to add components to it dynamically.
i.e i have added a combo box to the nested JPanel and based on the selection it makes, i want to add few more components to the same JPanel.

For this i created some code, but the problem is irrespective of the size, i mention for the combo box, it takes up the whole size of the JPanel and hence, the other components which needs to get added to the panel, based upon the selection of the combo box, are not getting added to the JPanel.

I searched through the net and understood that this has something to do with the layout manager only. But i am unable to debug this out..

Please help me with this

Note - while running the code, please enter the number of rows to be added as 1, since ive not yet handled multiple rows



Also, i would like to include multiple such panels arranged vertically growing downwards. Could someone suggest some idea for that.
The constraint in this case is, at first ill go and add 10 rows and then later depending on my requirement, ill go and delete a particular row in the middle (say 5th) using the delete button, which would be provided at last of every row, so the rows must get rearranged in order to fill the gap made by the removal on the middle row. Please help me with this too..

Thanks in advance

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you have too much of this stuff:

combo[inc].setSize(50, 50);
combo[inc].setBounds(20,160,70,100);

do you know what the lines do?

I'd suggest you start over, adding one component at a time, getting it in it's correct postion,
testing a listener if it needs one (simple System.out.println("OK"); to test), compiling/running etc.

if you get stuck you'll have a much simplified code to post.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Hello Harihara, and welcome to the Ranch! We don't have too many rules here, but we do ask that you BeForthrightWhenCrossPostingToOtherSites.
http://www.java-forums.org/awt-swing/51976-problem-adding-multiple-components-dynamically-nested-jpanel.html

edit: And http://www.codeguru.com/forum/showthread.php?p=2044723

luck, db
There are no new questions, but there may be new answers.
Harihara Sudan
Greenhorn

Joined: Sep 27, 2011
Posts: 2
Thanks for your reply,

Now i again tried starting it over and still facing the same issue.
Am, sorry if i am sounding crazy, since i am a newbie to this.i know i am messing up with the code.

As you had said, ive posted only the nested JPanel which is actually posing the problem

The main JPanle is named as Panel, which is added to a frame.
Since i need multiple panels within this Panel, i had used an array of JPanels within this Panel.
This array is the panels[].

And i thought the combo[inc].setSize and setBounds, does the same thing, where the latter in addition to setting the size fixes the x and y coordinate within the panel. I tried adding both since i thought either of that might work for me.

Now regardless of whatever i attempt to modify, the nested panel adds only the combo box and makes the combo box to fit the whole size of the frame and hence after selecting the choice, i am unable to add the textbox, through the function offerNameChange.

as you had suggested, i added the System.out.println to check the flow through the item listener, and the flow was completed till the end since all the messages were printed.



And i am sorry, actually i was having some timeline to show some progress and so i posted it in the other forum, but i didnt get any answer from the other one till now.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1077
    
  10

There are several issues with your code. First of all, you are not setting any layouts to the main panel - this means, the main panel uses FlowLayout. Do you know how FlowLayout handles the components? You just need to call setPreferredSize...

And, you are calling setBounds on the panel - I don't know how FlowLayout handles location of components, but logically, you are adding new panels to the same 'location' and with same size - so, you will have all panels one over the other...

and you should use actionListener for the combo box too unless you need to deal with old and new values (I think ItemListener fires events twice - once for old and new values).

I would suggest you to go through the layout tutorial first.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
 
 
subject: Adding components dynamically to a nested JPanel