Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: JComponent background colour

  1. #1
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default JComponent background colour

    Hey all, long time no see ;p

    Having not done any Java2D in many months now, I thought it was about time I got stuck right in.

    I know that you can set the background colour for a content pane easily through methods such as frame.getContentPane().setBackground(Color.BLACK), but as soon as I wanted to make my own custom content pane, contentpane.setBackground never seemed to work. Blissfully unaware of the reasons for it, the only way I've seemed able to get a background colour illusion to work is using the demonstration code below, where I simply draw a rectangle to cover the content pane.

    public class ContentPane extends JComponent {
     
        public ContentPane() {
            setBackground(Color.PINK);
        }
     
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());
            Graphics2D g2d = (Graphics2D) g;
     
            //Begin drawing
        }
     
        public static void main(String[] args) {
            JFrame gui = new JFrame("Window Title");
            ContentPane cp = new ContentPane();
     
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setSize(800, 600);
            gui.setResizable(false);
            gui.setLocationRelativeTo(null);
     
            gui.setContentPane(cp);
            gui.setVisible(true);
        }
    }

    Simply, what I'm asking is if there are any "formal" ways to do this; i.e. is there a way to get setBackground to work on a custom set CP? as without
    g.setColor(getBackground());
    g.fillRect(0, 0, getWidth(), getHeight());
    is just doesn't work.

    - Late night question of the week done! :3
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JComponent background colour

    By default, JComponents are not opaque, meaning that their backgrounds aren't painted. You can get around this by calling setOpaque(true) or by extending JPanel instead.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JComponent background colour

    Hey Kevin.
    I read that last night too, but when I called it in the constructor, it still doesn't paint the background.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JComponent background colour

    Update: Found a thread on Oracle's forums, and assuming the poster has his facts correct, it seems the implementation I set myself might be the way forward.

    JComponent doesn't do any custom painting, (not even the background) so you need to provide your own painting for the background.
    Or, extend a JPanel instead to take advantage of its background painting support.
    Source: https://forums.oracle.com/forums/thr...readID=1351373
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JComponent background colour

    Then I guess I was slightly wrong- not only are JComponents not opaque by default, but they can't be opaque at all. Extend JPanel instead.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JComponent background colour

    From the JComponent API:

    setBackground(Color bg):
    "Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property."
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JComponent background colour

    Ah right! Don't know how I missed that last night ;p

    Will stick with the JComponent implementation providing my own background in any case.
    Thanks Kevin.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Change colour of JComboBox arrow button
    By IanSawyer in forum AWT / Java Swing
    Replies: 0
    Last Post: April 21st, 2012, 04:05 PM
  2. Java programme to colour in a grid
    By why_always_me in forum Java Theory & Questions
    Replies: 20
    Last Post: March 29th, 2012, 06:09 AM
  3. Replies: 2
    Last Post: March 17th, 2012, 04:12 PM
  4. [SOLVED] Issue with mouse listeners in a custom JComponent
    By handuel in forum AWT / Java Swing
    Replies: 5
    Last Post: March 14th, 2012, 03:04 PM
  5. Replies: 10
    Last Post: November 16th, 2010, 12:12 AM