Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Redrawing JFrame

Posted on 2000-05-06
6
Medium Priority
?
584 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 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.
Suggested Courses

610 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