Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

setBackground in JFrame

Thread view: 
Pete - 13 Jan 2006 22:17 GMT
Killer Ap version 0.00002 seems to be avoiding the setBackground method
in JFrame,
I'd like to fix this - but more importantly, to understand *why* it
doesn't work. I suspect there's something fairly fundamental I've not
understood.

I have a class that extends JFrame, I use the class in an application
(does just the same in an applet though) and everything works as
advertised except that the background colour (OK, color) remains the
default pale grey (gray) *except that* (and this may be a clue) the red
background seems to flash on as the frame loads to the screen and if I
click and drag to resize, the red background is there in the newly
revealed screen area (but still not the original) until I release the
mouse button. setSize, setVisible, setDefaultCloseOperation work (I've
changed/removed them and put them back in to satisfy myself on this).

Why is setBackground different please?

I've chopped the code down to the bare (bear?) minimum below. I'm
running java version 1.5.0_06 under Windows XP and use a text-editor
and command-line for compile/execute.

All contributions gratefully received,

Pete

Code is:

import    javax.swing.*;
import  java.awt.*;

public class BasicFrame extends JFrame
{
    public BasicFrame()
    {
        super(" Test Frame");
        setSize(100,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.RED);
        setVisible(true);

    }    //  end cons()

}    // end class BasicFrame

public class TestHarness
{
    BasicFrame frame = new BasicFrame();

    public static void main(String[] arguments)
    {
        TestHarness harness = new TestHarness();
       
    }    // end main
}    // end TestHarness
Knute Johnson - 13 Jan 2006 23:04 GMT
> Killer Ap version 0.00002 seems to be avoiding the setBackground method
> in JFrame,
[quoted text clipped - 51 lines]
>     }    // end main
> }    // end TestHarness

Because what you really need to set the background on is the ContentPane
of the JFrame.

So in your BasicFrame class you need;

getContentPane().setBackground(Color.RED);

Signature

Knute Johnson
email s/nospam/knute/

James Westby - 13 Jan 2006 23:24 GMT
> Killer Ap version 0.00002 seems to be avoiding the setBackground method
> in JFrame,
[quoted text clipped - 51 lines]
>     }    // end main
> }    // end TestHarness

Hi,

Try this instead,

class BasicFrame extends JFrame
{
    public BasicFrame()
    {
        super(" Test Frame");
        setSize(100,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        content.setBackground(Color.RED);
        setVisible(true);

    }   //  end cons()

}   // end class BasicFrame

Setting the background on the contentPane() seems to have more effect.
There are several layers to swing components and i guess the
setBackground() method sets the colour of one further back, and this
becomes visible as it redraws itself, but the contentPane is normally in
front of it.

James
Thomas Hawtin - 14 Jan 2006 00:58 GMT
> Setting the background on the contentPane() seems to have more effect.
> There are several layers to swing components and i guess the
> setBackground() method sets the colour of one further back, and this
> becomes visible as it redraws itself, but the contentPane is normally in
> front of it.

JFrame is a heavyweight component (a subclass of Frame). Setting its
background colour may determine what the operating system paints,
outside of the Event Dispatch Thread (EDT, the one thread Swing
components keep to). Swing will spend some time painting to an
off-screen buffer, before blitting across. During this time, the
heavyweight component may flash.

An alternative is to changing the content pane's colour, is to make the
content pane transparent. On some versions of some PL&Fs, the content
pane will be transparent by default. However, making components
transparent may well impact performance.

Mustang (Java SE 6) is better optimised, so you are less likely to see
the flash.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 14 Jan 2006 03:45 GMT
>I have a class that extends JFrame, I use the class in an application
>(does just the same in an applet though) and everything works as
[quoted text clipped - 4 lines]
>revealed screen area (but still not the original) until I release the
>mouse button.

 jframe.getContentPane().setBackground( Color.YELLOW );

See http://mindprod.com/jgloss/jframe.html for details.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Pete - 14 Jan 2006 20:36 GMT
Thanks for the help on this one. You all came up with the same solution
- and you're all right :)

Annoyingly, I did know this (I said I'd picked up Java before). What
threw me was that I'd picked up on the following from the Sun tutorial:

"Version Note: We anticipate that in v1.5 invoking add on a top-level
container will have the same effect as invoking it on the top-level
container?s content pane. "

This is true, of course, and where previously I'd 'add'd layout
managers and components to a contentPane, now I can add directly to the
container - conceptually, I'd bundled setBackground into the same
pattern. James' comment about Swing having 'layers' helps me reason
about this.

So thanks all. Problem solved.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.