Solved

Redrawing JFrame

Posted on 2000-05-06
6
575 Views
Last Modified: 2012-08-14
I have an application written in JB3. At some point of execution, I call invalidate() method of the main window (which in this case extends JFrame) but it doesn't do anything. Why is that?

Also: I tried setBackground() method on the same main window. It works fine on Buttons and such, but on the JFrame it does the following: it changes the color for an instant (I can see the flicker) and then changes it back to light gray. Why is that?
0
Comment
Question by:Lescha
  • 2
  • 2
  • 2
6 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2784164
The Component methods invalidate and validate are about ensuring that the component has a valid layout. They don't necessarily force the component to be redrawn. What you should do is also call repaint(). This will ensure that the component is redrawn. I've often found it necessary to call these methods (validate first) together.

As for JFrame background, while it is true that JFrame extends Component and so has a setBackground method, it is JFrame's content pane that you are actually viewing. The flicker is due to the fact that JFrame tries to draw its background and then redraws its children which causes its content pane to draw over. Call setBackground on the JFrame's content pane and that will fix this problem:
    frame.getContentPane().setBackground(Color.green);

Best regards,
Jim Cakalic
0
 
LVL 1

Author Comment

by:Lescha
ID: 2784184
Thanks, please lock the question!
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2784201
Jim, isn't the content pane transparent?
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 7

Expert Comment

by:Sasha_Mapa
ID: 2784202
Lescha: you can click on the "Accept Comment as answer" on the title bar of Jim's comment.
0
 
LVL 1

Author Comment

by:Lescha
ID: 2784231
There is no such button on it. Don't know why, it is usually there.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 20 total points
ID: 2784240
Answer: call JFrame.repaint() and use JFrame.getContentPane().setBackground() to set background color of a JFrame.

Sasha, apparently JFrame.contentPane is opaque. The following code works and the line marked 'prints true' at the bottom of the constructor actually prints 'true' on my system. I'm running jdk1.3rc3 -- but I don't think this has changed. Nope. Just checked jdk1.2 and it works the same there.

---------- JFrameBackground.java ----------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class JFrameBackground {
    JFrame frame;

    public JFrameBackground() {
        frame = new JFrame("Background test");
        frame.getContentPane().setLayout(new GridBagLayout());
        frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        JButton green = new JButton("Green");
        green.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    frame.getContentPane().setBackground(Color.green);
                }
            });
        frame.getContentPane().add(green);
        JButton red = new JButton("Red");
        red.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    frame.getContentPane().setBackground(Color.red);
                }
            });
        frame.getContentPane().add(red);

        frame.setSize(400,300);
        frame.show();

        // following prints true
        System.out.println(frame.getContentPane().isOpaque());
    }

    public static void main(String[] args) {
        JFrameBackground app = new JFrameBackground();
    }

}
---------- end ----------

Best regards,
Jim Cakalic
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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

912 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

16 Experts available now in Live!

Get 1:1 Help Now