We help IT Professionals succeed at work.

SWING: Replace a JFrame's pre-existing JPanel with a different JPanel

fshtank asked
Last Modified: 2013-11-23
Hi everyone,

I have a selection list that will drive the contents of a particular JPanel.  
This window is in MS Outlook / E-mail format (3 sections):  
   1) vertical list  
   2) Selection Criteria (Search)  
   3)  Output Results

Mouse click a JList item (section #1)  to put the appropriate search criteria options in the JPanel.

I have created a separate external JPanel object for each criteria setup.

    private void lstReportListMouseClicked(java.awt.event.MouseEvent evt) {
        idx = lstReportList.getSelectedIndex()
        . . .   get clicked objects String Value . . .        
            if (stringVal.equals("Next Chosen Criteria")) {

                // I want to replace the current Criteria JPanel (Section #2) with the new Corresponding JPanel here.
                // I have tried the Following:
                panelrptCriteria.add(new nextCriteriaPanel());
                panelrptCriteria = new nextCriteriaPanel();

The Jpanel does not refresh correctly to show the search options.
What do I need to do here?  I am stuck.

I am using Matisse with MyEclipseIDE to generate the SWING code.  The code above is my own custom code in a JList event.

Thanks for your help.

Watch Question

Java Developer
Top Expert 2010
This one is on us!
(Get your first solution completely free - no credit card required)
Mick BarryJava Developer
Top Expert 2010



Card layout would have me instantiate each JPanel at construction and then set visible(true/false) as needed correct?

That seems kind of heavy.  The intention of this list is for it to grow - so if I have up to 10 even 20 search options, that seems a bit 'big.'

I tried revalidate and it didn't cause the JPanel to be drawn.
I also tried repaint on the window itself - that also did not draw the JPanel.

            if (stringVal.equals("Next Chosen Criteria")) {
                panelrptCriteria = new nextCriteriaPanel();

        this.repaint();  // repaint the JFrame

Any other thoughts?
- Thanks
Mick BarryJava Developer
Top Expert 2010

> Card layout would have me instantiate each JPanel at construction and then set visible(true/false) as needed correct?

you'd create them up front, but use the show() method of the layout manager not setVisible()

> That seems kind of heavy.

not really, you'll find creating/adding/removing panels as required is going to put more load on your app

>         panelrptCriteria.revalidate();

u need to revalidate() the panel you are adding to, not the panel you added.


This will simplify things in the short term.


Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.