Solved

whats wrong with this swing code?

Posted on 2004-10-20
14
468 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

762 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