Solved

Dynamically adding a panel to a Swing GUI

Posted on 2002-03-13
2
293 Views
Last Modified: 2013-11-23
Hi,

When I press a JButton I want to add a panel panel to my UI.  How would I implement this?

Thanks,

Matt
0
Comment
Question by:MatthewL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 200 total points
ID: 6861508
Here is an example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestFrame extends JFrame {
   public TestFrame() {
      addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(1);
        }
      });

      JButton b = new JButton("Click me");
      b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
           JPanel p = new JPanel();
           p.add(new JTextField(15));

           JButton button = (JButton)ae.getSource();
           Container c = button.getParent();
           c.add(p);
           pack();
        }
      });

      getContentPane().setLayout(new FlowLayout());
      getContentPane().add(b);
      pack();
      show();
   }

   public static void main(String[] args) {
       new TestFrame();      
   }
}
0
 
LVL 1

Author Comment

by:MatthewL
ID: 6862793
Hi pkwan.

Perfect!

Thanks,

Matt
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question