• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • 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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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