• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 535
  • Last Modified:

My JFrame does not refresh!!

Hi guys

I currently have problems refreshing my JFrame. It gets blurred when I move some other windows over it.

mainFrame.getContentPane().add("The object I want to add);
((JComponent)mainFrame.getContentPane()).validate();
mainFrame.getContentPane().repaint();

I wonder whether it is correct.
When I set super.paint(), my object will refresh BUT the second object which I draw using the same method as above will not be shown.

What can i do?
I tried using paintComponent and repaint but it is no use.

Please help!
0
ricteo
Asked:
ricteo
  • 6
  • 6
1 Solution
 
sciuriwareCommented:
How about mainFrame.setVisible(true);

;JOOP!
0
 
ricteoAuthor Commented:
Cannot work lah. I thought setVisible is just to make the JFrame visible?
0
 
sciuriwareCommented:
It also boosts display when components have been added.
B.t.w. I always fill my mainframes with JPanels etc. that I may fill later with components.
In situations where I change a lot to a JPanel I practise:

   /**
    * Destroys the display after completion.
    */
   public void destroyComponent()
   {
      SwingUtilities.invokeLater
      (
         new Runnable()
         {
           public void run()
           {
              paneXyz.remove(thatComponent);
              paneXyz.revalidate();
              paneXyz.repaint();
           }
         }
      );
   }

It's also possible to make your class a Thread extension to have more access to references.
This construct is only needed when the action is in the display thread: from a button or timer.

;JOOP!
0
Technology Partners: 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!

 
ricteoAuthor Commented:
Hi

Is the paneXyz the Jframe?

Thanks
0
 
sciuriwareCommented:
paneXyz (is a pane!) is the JPanel where you once added the component:

        paneXyz.add(thatComponent); ---> paneXyz.remove(thatComponent);

;JOOP!
0
 
ricteoAuthor Commented:
Hi

But I am using getContentPane().add(thatComponent);

How?
0
 
sciuriwareCommented:
Better place separate components in a JPanel that, if it's alone, sits in BorderLayout.CENTER of the contentpane.

;JOOP!
0
 
ricteoAuthor Commented:
What do you mean?
Do you mean it could be because i did not specify which part of the Jpanel  it should display in?


Thanks
0
 
sciuriwareCommented:
No, you should not place components directly into the contents pane.
;JOOP!
0
 
ricteoAuthor Commented:
Oh, Do you mean that I should have a JPanel which I should put my components into and then place the JPanel into the content pane?

Thanks
0
 
sciuriwareCommented:
Yes that's what I said all the time.
And most of the examples in books and tutorials work that way.
;JOOP!
0
 
ricteoAuthor Commented:
Ok. I go and try it out. I will give you your points if it works!
Thanks
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.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now