[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Dynamically adding a panel to a Swing GUI

Posted on 2002-03-13
2
Medium Priority
?
296 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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

656 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