Solved

My JFrame does not refresh!!

Posted on 2004-09-12
12
529 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
egit plugin on eclipse 8 82
How to convert String matching to regex in java 4 48
How do I remove an object from a 3 40
Eclipse Java import and method not resolved 4 43
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…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

765 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