0

I am trying to make a program with buttons, that when you click them, change the background color of the frame

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ColorFrame {

public static void main(String[] args){

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setSize(300, 200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JButton redButton = new JButton ("Red");
    final JButton greenButton = new JButton ("Green");
    final JButton blueButton = new JButton ("Blue");

    class Listener extends JPanel implements ActionListener{

        public void actionPerformed(ActionEvent event) {
            Color color;
            if (event.getSource() == redButton){
                color = Color.red;                  
            } else if (event.getSource() == greenButton){
                color = Color.green;
            } else {
                color = Color.blue;
            }
            setBackground(color);
            System.out.println(color);
            repaint();
        }           
    }

    redButton.addActionListener(new Listener());
    greenButton.addActionListener(new Listener());
    blueButton.addActionListener(new Listener());

    panel.add(new JButton ("Red")); 
    panel.add(new JButton ("Green"));
    panel.add(new JButton ("Blue"));
    frame.add(panel);       


}

}

Yet when I click the buttons, nothing seems to happen and I think it might have something to do with the listeners not being activated for reason

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
manis
  • 731
  • 1
  • 13
  • 24

4 Answers4

3

You define the buttons here:

 final JButton redButton = new JButton ("Red");
 final JButton greenButton = new JButton ("Green");
 final JButton blueButton = new JButton ("Blue");

But then you add entirely new buttons to the actual panel so the buttons with the listeners attached are never added:

panel.add(new JButton ("Red")); 
panel.add(new JButton ("Green"));
panel.add(new JButton ("Blue"));

You should add the buttons like this:

 panel.add(redButton);
 panel.add(greenButton);
 panel.add(blueButton);
LadyBernkastel
  • 447
  • 4
  • 13
  • The listener is activated now, but the background color still does not change – manis Jan 06 '14 at 21:01
  • I think the issue is that you're setting the background within the listener and actually setting the background colour of the listener - not the button. You should take a look at [link](http://stackoverflow.com/questions/14627223/how-to-change-a-jbutton-color-on-mouse-pressed) to see how it's been done before or keep a reference to the button which you can access from the listener and do `redButton.setBackground()` or `greenButton.setBackground()`. – LadyBernkastel Jan 06 '14 at 21:17
  • 1
    `setBackground` (in this context) will set the "frames" background. The frame has a `JRootPane` on it, which as a `JLayerdPane` on it, which has a "content pane" on it, which finally has the OP's panel onto of that. So while the frame's background color might be changing, there is no way that it would ever be possible that the user would be able to see it. Instead, they should change the color of the `JPanel` which they place onto the content pane...+1 by the way ;) – MadProgrammer Jan 06 '14 at 21:23
3

Take a moment to visualise you setup...

You have a JFrame. This window has a JRootPane, which contains a JLayerdPane, which contains a the "content pane".

RootPane

The content pane is generally the most top level component of a basic window.

On to this, you add a JPanel. JPanel is opaque by default. By default, the content pane uses a BorderLayout, this means that anything added to the default position will be placed in the CENTER position, filling the available space...

This means, you frame is covered by a JLayeredPane, content pane AND your JPanel. setBackground does not delegate to the content pane like some of the other methods, but, in your case, it wouldn't help, as the JPanel you add is now covering it...

In addition to LadyRacheya suggestions, you have two choices.

You can make the JPanel transparent...

JPanel panel = new JPanel();
panel.setOpaque(false);

And change the background color of the content pane...

getContentPane().setBackground(color);

OR you can simply change the background color of the JPanel....

final JPanel panel = new JPanel();

//...

panel.setBackground(color);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Try this:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ColorFrame {

public static void main(String[] args){

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setSize(300, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JButton redButton = new JButton ("Red");
    final JButton greenButton = new JButton ("Green");
    final JButton blueButton = new JButton ("Blue");

    class Listener extends JPanel implements ActionListener{

        public void actionPerformed(ActionEvent event) {
            Color color;
            if (event.getSource() == redButton){
                color = Color.red;
                redButton.setBackground(color);
                panel.setBackground(color);//To set panel background instead of frames background
            } else if (event.getSource() == greenButton){
                color = Color.green;
                greenButton.setBackground(color);
                panel.setBackground(color);
            } else {
                color = Color.blue;

                blueButton.setBackground(color);
                panel.setBackground(color);  
            }
            setBackground(color);
            System.out.println(color);
            repaint();
        }           
    }

    redButton.addActionListener(new Listener());
    greenButton.addActionListener(new Listener());
    blueButton.addActionListener(new Listener());

    panel.add(redButton); 
    panel.add(greenButton);
    panel.add(blueButton);
    frame.add(panel);       
frame.setVisible(true);


}

}
ravibagul91
  • 20,072
  • 5
  • 36
  • 59
-2
public class ColorFrame {

public JPanel panel;
public static void main(String[] args){

    JFrame frame = new JFrame();
    final JPanel panel = new JPanel();
    frame.setSize(300, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JButton redButton = new JButton ("Red");
    final JButton greenButton = new JButton ("Green");
    final JButton blueButton = new JButton ("Blue");

    class Listener extends JPanel implements ActionListener{

        public void actionPerformed(ActionEvent event) {
            Color color;
            if (event.getSource() == redButton){

                redButton.setBackground(Color.RED);
                panel.setBackground(Color.RED);

            } else if (event.getSource() == greenButton){

                greenButton.setBackground(Color.GREEN);
                panel.setBackground(Color.GREEN);

            } else {

                blueButton.setBackground(Color.BLUE);
                panel.setBackground(Color.BLUE);  
            }

            setBackground(Color.WHITE);
            System.out.println(Color.WHITE);
            repaint();
        }           
    }

    redButton.addActionListener(new Listener());
    greenButton.addActionListener(new Listener());
    blueButton.addActionListener(new Listener());

    panel.add(redButton); 
    panel.add(greenButton);
    panel.add(blueButton);
    frame.add(panel);       
    frame.setVisible(true);
}
bigGuy
  • 1,732
  • 1
  • 22
  • 37