Solved

My JFrame does not refresh!!

Posted on 2004-09-12
12
525 Views
Last Modified: 2012-08-14
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
Comment
Question by:ricteo
  • 6
  • 6
12 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 12038325
How about mainFrame.setVisible(true);

;JOOP!
0
 

Author Comment

by:ricteo
ID: 12038408
Cannot work lah. I thought setVisible is just to make the JFrame visible?
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 60 total points
ID: 12039274
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
 

Author Comment

by:ricteo
ID: 12045473
Hi

Is the paneXyz the Jframe?

Thanks
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12052775
paneXyz (is a pane!) is the JPanel where you once added the component:

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

;JOOP!
0
 

Author Comment

by:ricteo
ID: 12053401
Hi

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

How?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 24

Expert Comment

by:sciuriware
ID: 12054763
Better place separate components in a JPanel that, if it's alone, sits in BorderLayout.CENTER of the contentpane.

;JOOP!
0
 

Author Comment

by:ricteo
ID: 12055850
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 12061756
No, you should not place components directly into the contents pane.
;JOOP!
0
 

Author Comment

by:ricteo
ID: 12065821
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 12067539
Yes that's what I said all the time.
And most of the examples in books and tutorials work that way.
;JOOP!
0
 

Author Comment

by:ricteo
ID: 12072030
Ok. I go and try it out. I will give you your points if it works!
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

929 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now