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 11 of 11

Thread: How to change the background colour by clicking buttons?

  1. #1
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to change the background colour by clicking buttons?

    Hi guys

    Below code mean to change the background colour by clicking the redButton or blueButton, but the code doesn't work, (only can show the frame and two buttons), does any anyone know why?

    Thanks heaps

    package brainydraw;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class PictureButton extends JFrame
    {
     
        private JButton redButton;
        private JButton blueButton;
     
        public PictureButton()
        {
            setSize(200, 200);
            setLocation(200, 200);
     
            redButton = new JButton("RED");
            blueButton = new JButton("BLUE");
     
            Container content = getContentPane();
            content.setLayout(new FlowLayout());
     
            content.add(redButton);
            content.add(blueButton);
     
            //register the current panel as listener for the buttons
            ActionListener rBListener = new RedAndBlueButtonListener();
            redButton.addActionListener(rBListener);
            blueButton.addActionListener(rBListener);
        }
     
           public class RedAndBlueButtonListener implements ActionListener
           {
               public void actionPerformed(ActionEvent ae)
               {
                    Color color = getBackground();  // color will be set
                    Object source = ae.getSource();
                    if (source == redButton) color = Color.red;
                    else if (source == blueButton) color = Color.blue;
                    setBackground(color);
                    repaint();
                }
           }
     
        public static void main(String[] args)
        {
            JFrame f = new PictureButton();
     
            f.addWindowListener(new WindowAdapter()
            {
     
                public void windowClosing(WindowEvent we)
                {
                    System.exit(0);
                }
            });
            f.setVisible(true);
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to change the background colour by clicking buttons?

    Can you elaborate on what you mean by 'code doesn't work'?

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: How to change the background colour by clicking buttons?

    You're setting the background of a JFrame. But the background of a JFrame is covered by its contentPane, which is by default opaque. You should be setting the background of the contentPane.

    Oh, and it's never necessary to explicitly call repaint() after changing the background of a Swing component.

    db

  4. #4
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to change the background colour by clicking buttons?

    Hi db
    Thank you for your suggestion, it makes great sense, I guess below part of code caused the colour to be set on the frame instead of the contenPanel, am I right? But how exactly do I modify it?
    Thanks again

    public class RedAndBlueButtonListener implements ActionListener
           {
               public void actionPerformed(ActionEvent ae)
               {
                    Color color = getBackground();  // color will be set
                    Object source = ae.getSource();
                    if (source == redButton) color = Color.red;
                    else if (source == blueButton) color = Color.blue;
                    setBackground(color);
                    repaint();
                }
           }
    Last edited by ice; November 14th, 2010 at 10:17 PM.

  5. #5
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to change the background colour by clicking buttons?

    Quote Originally Posted by copeg View Post
    Can you elaborate on what you mean by 'code doesn't work'?
    Hi copeg

    I got a problem that I when I clicked the buttons, the colours of the background is not changed accordingly as it suposed to be, db suggest the colour is set on the frame which is hidden behind the panel, so is there any way I can dispaly colour without using panel?

    Thanks heaps

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: How to change the background colour by clicking buttons?

    frame.getContentPane().setBackground(...)
    db

  7. #7
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to change the background colour by clicking buttons?

    Thanks db
    I have added getContentPane(). in front of setBackground(color) asp below code, then ran it, but got a compiling error:

    Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
    at java.awt.Container.checkNotAWindow(Container.java: 431)
    at java.awt.Container.addImpl(Container.java:1039)
    at java.awt.Container.add(Container.java:365)
    at brainydraw.PictureButton.main(PictureButton.java:7 8)
    Java Result: 1


    Any more thought?

    class RedAndBlueButtonListener implements ActionListener
           {
     
               public void actionPerformed(ActionEvent ae)
               {
                    Color color = getBackground();  // color will be set
                    Object source = ae.getSource();
                    if (source == redButton) color = Color.red;
                    else if (source == blueButton) color = Color.blue;
                    getContentPane().setBackground(color);
                    repaint();
                }
           }

  8. #8
    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: How to change the background colour by clicking buttons?

    If you want help, you'll have to post an SSCCE that demonstrates the problem. The code you posted doesn't seem to have anything to do with the error you posted.

  9. #9
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to change the background colour by clicking buttons?

    Hi Kevinworkman

    Thanks for the advice, yes I need the help if you can give some. My whole code is asp below, it doesn't compile after I added " getContentPane" into the method "public class RedAndBlueButtonListener implements ActionListener". All I wanted to do is to click the buttons to change background colour.

    Any idea?


    package brainydraw;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class PictureButton extends JFrame
    {
     
        private JButton redButton;
        private JButton blueButton;
     
        public PictureButton()
        {
            setSize(200, 200);
            setLocation(200, 200);
     
            redButton = new JButton("RED");
            blueButton = new JButton("BLUE");
     
            Container content = getContentPane();
            content.setLayout(new FlowLayout());
     
            content.add(redButton);
            content.add(blueButton);
     
            //register the current panel as listener for the buttons
            ActionListener rBListener = new RedAndBlueButtonListener();
            redButton.addActionListener(rBListener);
            blueButton.addActionListener(rBListener);
        }
     
           public class RedAndBlueButtonListener implements ActionListener
           {
               public void actionPerformed(ActionEvent ae)
               {
                    Color color = getBackground();  // color will be set
                    Object source = ae.getSource();
                    if (source == redButton) color = Color.red;
                    else if (source == blueButton) color = Color.blue;
                    getContentPane.setBackground(color);
                    repaint();
                }
           }
     
        public static void main(String[] args)
        {
            JFrame f = new PictureButton();
     
            f.addWindowListener(new WindowAdapter()
            {
     
                public void windowClosing(WindowEvent we)
                {
                    System.exit(0);
                }
            });
            f.setVisible(true);
        }
    }

  10. #10
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to change the background colour by clicking buttons?

    Hi guys
    Thank you all, I just solved this problem. hehe...

  11. #11
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: How to change the background colour by clicking buttons?

    getContentPane is a method, but you are writing code that treats it as a variable. Go back to the one line of code I posted and see how your code differs from it.

    edit The earlier method call in the constructor is correct. The mistake I refer to is in the actionPerformed(...)

    Also, learn to respect Swing's single threaded rule and create and modify Swing components only on the EDT.
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    Last edited by Darryl.Burke; November 16th, 2010 at 12:16 AM.

Similar Threads

  1. background
    By b109 in forum AWT / Java Swing
    Replies: 0
    Last Post: May 24th, 2010, 06:37 AM
  2. unable to go to next page...while clicking submit button
    By javaking in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: April 29th, 2010, 02:55 AM
  3. Background image on GUI
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 3
    Last Post: March 5th, 2010, 12:10 PM
  4. java is not clicking for me, why?
    By cejay in forum Java Theory & Questions
    Replies: 1
    Last Post: February 28th, 2010, 12:57 PM
  5. How to upload a file by clicking a link instead of button?
    By raghuprasad in forum Java Theory & Questions
    Replies: 2
    Last Post: May 3rd, 2009, 05:21 AM