?
Solved

whats wrong with this swing code?

Posted on 2004-10-20
14
Medium Priority
?
471 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
[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
  • 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 1500 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

770 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