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
Solved

whats wrong with this swing code?

Posted on 2004-10-20
14
467 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal Reports Licensing Questions 4 34
Notify sent to other threads in Java 9 33
Java 8 to Java 6 8 20
Java Eclipse Loop 3 20
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 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