Link to home
Start Free TrialLog in
Avatar of kenshaw
kenshaw

asked on

whats wrong with this swing code?

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?
Avatar of kenshaw
kenshaw

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of msterjev
msterjev

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
Give here ur code, I'll correct it .
I already answered the question!
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.
Avatar of zzynx
Sure you mean
>>      appMainFrame.setContentPane(new aboutSoftware().aboutSoftwarePanel);
and not
           appMainFrame.getContentPane().add(new aboutSoftware().aboutSoftwarePanel);
?
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!
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...
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.
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!
>> 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.
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!
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();
                  }
         });
    }