?
Solved

Redrawing JFrame

Posted on 2000-05-06
6
Medium Priority
?
582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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