Solved

Redrawing JFrame

Posted on 2000-05-06
6
574 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

708 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

15 Experts available now in Live!

Get 1:1 Help Now