1,105,320 Community Members

adding multiple panel in frame

Member Avatar
sj5536
Junior Poster in Training
63 posts since Sep 2010
Reputation Points: 5 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

how do i add multiple pane in frame dynamically that is in my frame there is one text box which take value from user when user click on ok button i have to add that much panel in jframe how do i do that ?

Member Avatar
WolfShield
Posting Whiz in Training
260 posts since Oct 2010
Reputation Points: 32 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

So your question is,
since you can't add a text field strait into a JFrame, how do you add a JPanel into the JFrame so that you can display the JTextField in it?

If this is what you mean, then here is the solution.

You need three things:

1) the JFrame
2) a JPanel
3) the JTextField

I'm assuming you've already created the JFrame (e.g.)

JFrame mainWin = new JFrame("Main Window");
mainWin.setVisible(true);

For the second part you need to add a content pane.

The content pane is what holds all content in the window. You can even put other JPanels in the content pane.

Here's how to create the content pane:

JPanel content = new JPanel();
content.setLayout(new GridBagLayout()); // You can use whatever layout manager you want

This created the content pane, now you need to add it the the window:

JFrame mainWin = new JFrame("Main Window");
mainWin.setVisible(true);
mainWin.setContentPane(content); // This adds the JPanel 'content' to the Main Window

Now you just need to created the JTextField and add it to the JPanel content.

I'm not going to go into the whole java layout thing here, but here is how you add it.

JTextField userInput = new JTextField(20);
// Code saying where to add the JTextField in the content pane
content.add(userInput);

Hope this helps,


- WolfShield

Member Avatar
WolfShield
Posting Whiz in Training
260 posts since Oct 2010
Reputation Points: 32 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

Oops,
forgot about the 'multiple panels' part. :S

So like I said in the last post, add the content pane.

Then create another JPanel and add the JTextField to it.

JPanel textfieldPanel = new JPanel();
textfieldPanel.add(userInput);

Then add the textfieldPanel to the content pane.

content.add(textfieldPanel);

Then you can just repeat the process with other JPanels.

Once again, hope this helps,

- WolfShield

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,345 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

good conception required, followed steps

.
.
.
content.setPreferredSize(new Dimmension(800,600))
.
.
content.add(someComponents)
.
.
//last lines would be
JFrame mainWin = new JFrame("Main Window");
mainWin.add(content, BorderLayout.CENTER);
mainWin.pack();
mainWin.setVisible(true);
Member Avatar
Punit.Shah
Newbie Poster
1 post since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

How do I add multiple JPanel on single JFrame dynamically?
I mean if user clicks one of the menu item then the related JPanel should be open.
Or if you can suggest me better way of doing the same, would be appreciated.
I have GUI, which have many MenuItem, how do I open multiple JPanels on user's click on one of the Menu Item.?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article