Solved

whats wrong with this swing code?

Posted on 2004-10-20
14
463 Views
Last Modified: 2013-11-23
I've got a simple java application with a single JFrame as the top level container.

When the app starts up, the contentPane of the JFrame is set with:
initialiseMenuBar(mainFrame);
mainFrame.setJMenuBar(objJMenu);
mainFrame.setContentPane(objGuiPanels.helpForm);

where helpForm is a JPanel inside the objGuiPanels object.

I then have a series of menus, and the action on a menu button calls the following method.  What i'm trying to do is replace
the JPanel that the app loads at startup with a JPanel that corresponds to the menu item that the user has clicked.  In this code aboutSoftware is a class, and aboutSoftwarePanel is a public JPanel in it.

 public void openHelpPage()
    {
        System.out.println("should change form");

        // NOTE THIS METHOD IS CAUSING ERROR - THE CONTENT PANE ISnT CHANGING
        // IT IS FREEZING UP AND NOT REPAINTING
        appMainFrame.setContentPane(new aboutSoftware().aboutSoftwarePanel);

    }

The content pane doesn't change however - it sort of freezes and doesn't repaint itself.  I can keep using the menus, so its not actually frozen, but the content pane stops repainting and it doesn't load the new JPanel.

any ideas?
0
Comment
Question by:kenshaw
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 4

Author Comment

by:kenshaw
ID: 12366849
note that the system is printing out "should change form" so there's nothing wrong with the execution logic at this point.... - it just causes a problem when i try to reset the content pane to the new JPanel
0
 
LVL 3

Accepted Solution

by:
msterjev earned 500 total points
ID: 12366915
After setting the pane call:

appMainFrame.invalidate();

or

appMainFrame.validate();
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12366932
Give here ur code, I'll correct it .
0
 
LVL 3

Expert Comment

by:msterjev
ID: 12366981
I already answered the question!
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12367019
If not solved you can do something like this:
getContentPane().remove(oldPanel);
newPanel=new JPanel();
getContentPane().add(newPanel, java.awt.BorderLayout.CENTER);
and finally call pack() over the frame

Hope this could help you.
Bye, Giant.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12367021
Sure you mean
>>      appMainFrame.setContentPane(new aboutSoftware().aboutSoftwarePanel);
and not
           appMainFrame.getContentPane().add(new aboutSoftware().aboutSoftwarePanel);
?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 24

Expert Comment

by:sciuriware
ID: 12367430
There is only one correct way to remove a JPanel from its holder:

      SwingUtilities.invokeLater
      (
         new Runnable()
         {
           public void run()
           {
              Holder.remove(panel);
              Holder.remove.revalidate();
              Holder.remove.repaint();
           }
         }
      );
"Holder" is the component you once added "panel" to.

;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12367465
Think you made a typo:
    Holder.remove(panel);
    Holder.revalidate();        // no remove ;°)
    Holder.repaint();            // no remove ;°)

Is that SwingUtilities.invokeLater stuff really needed?
I always do it without, without problems...
0
 
LVL 3

Expert Comment

by:falter
ID: 12367516
If your screen freezes this normaly a problem with the event queue and running the code in the correct thread, because Swing isn't thread safe you may corrupt the queue and than your application freeze.
Try SwingUtilities , this shall avoid problems with the event queue.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12367519
I had severe non-painting problems until 'objects' tought me this one.
It's a solution for everything fired from the wrong thread.
And if from the main thread, it doesn't harm.

Questioners sometimes forget to mention those circumstances when they don't post their full code.
;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12367596
>> And if from the main thread, it doesn't harm.
But then it isn't needed. I see.
That's why it works for me without.
I don't "mess with threads" (yet) ;°)

btw: don't know if the author will be able to use revalidate().
It's a JComponent function, while getContentPane() returns a Containter.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12367650
1) typically such actions will be fired from menus or buttons (or even timers),
2) Thou shall not put simple components into the contentpane but into JPanels
or such; if you want to keep sizes and the sort....
;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12367854
Conclusion for kenshaw:

>> When the app starts up, the contentPane of the JFrame is set with:
>>initialiseMenuBar(mainFrame);
>>mainFrame.setJMenuBar(objJMenu);
>>mainFrame.setContentPane(objGuiPanels.helpForm);
Replace the last line with:

    mainPanel = new JPanel( new BorderLayout() );    // mainPanel is a private JPanel variable
    mainFrame.getContentPane().add(mainPanel);

Then openHelpPage() could look like:

public void openHelpPage() {
        SwingUtilities.invokeLater( new Runnable() {
                  public void run() {
                       mainPanel.removeAll(); // remove previous child components if any
                       mainPanel.add( new aboutSoftware().aboutSoftwarePanel, BorderLayout.CENTER );
                       mainPanel.revalidate();
                       mainPanel.repaint();
                  }
         });
    }

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now