Link to home
Start Free TrialLog in
Avatar of fshtank
fshtankFlag for United States of America

asked on

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

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.

Code:
    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.









ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fshtank

ASKER

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

        panelrptCriteria.setVisible(true);
        panelrptCriteria.revalidate();
        this.repaint();  // repaint the JFrame

Any other thoughts?
- Thanks
> 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.
Avatar of fshtank

ASKER

This will simplify things in the short term.

Thanks.