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 ?

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

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

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);

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.?

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.