Solved

Redrawing JFrame

Posted on 2000-05-06
6
578 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Weekend adv creator 3 64
servlet example 17 59
runtime exception 2 50
Java Restore security prompts not working 10 140
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

726 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