I have to create a GUI that when I click on the buttons it changes the color of the background. Below is what I have but I can't figure out what code to put in and where to change the color of the background.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ColorViewer extends JFrame
{  
   public ColorViewer()
   {  
      colorPanel = new JPanel();
      
      add(colorPanel, BorderLayout.CENTER);
      createControlPanel();
      setSampleColor();
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

   public void createControlPanel()
   {
      class ColorListener implements ChangeListener
      {  
         public void stateChanged(ChangeEvent event)
         {  
            setSampleColor();
         }
      }   

      ChangeListener listener = new ColorListener();

      redButton = new JButton("Red");
      redButton.addChangeListener(listener);

      greenButton = new JButton("Green");
      greenButton.addChangeListener(listener);

      blueButton = new JButton("Blue");
      blueButton.addChangeListener(listener);
      
      JPanel controlPanel = new JPanel();
      controlPanel.setLayout(new GridLayout(3, 2));

      controlPanel.add(new JLabel("Red"));
      controlPanel.add(redButton);

      controlPanel.add(new JLabel("Green"));
      controlPanel.add(greenButton);

      controlPanel.add(new JLabel("Blue"));
      controlPanel.add(blueButton);

      add(controlPanel, BorderLayout.SOUTH);      
   }
   
   /**
      Reads the slider values and sets the panel to
      the selected color.
   */
   public void setSampleColor()
   {  

      // Set panel background to selected color
            colorPanel.repaint();
   }
   
   public void actionPerformed(ActionEvent e)
   {
	   Object o = e.getSource();
	   
	   if(o == redButton){/*set color to red*/}
	   else if(o == greenButton){/*set color to green*/}
	   else if(o == blueButton){/*set color to blue*/}
	}
   
   
   private JPanel colorPanel;
   private JButton redButton;
   private JButton greenButton;
   private JButton blueButton;

   private static final int FRAME_WIDTH = 300;
   private static final int FRAME_HEIGHT = 400;
}
import javax.swing.JFrame;

public class ColorViewerMain
{  
   public static void main(String[] args)
   {  
      ColorViewer frame = new ColorViewer();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

Use an ActionListener instead of ChangeListener for your buttons. You can pass the color you wish to change to as a parameter to your setSampleColor() method. Set the panel background to that color before repaint.

I changed it to ActionListener but now I'm getting an error for ColorListener. What do I need to change that to?

I guess that would depend on what the error was...

I changed it to ActionListener but now I'm getting an error for ColorListener. What do I need to change that to?

I think because your method in the ColorListener class is stateChanged(), make it actionPerformed().You can write this code with a lot less if you want. How I recommend doing it is build your GUI in the constructor then create a class that implements ActionListener. For example:

private class ColorListener implements ActionListener {

Then a method for the action performed(the button click) in the ColorListener class. For example:

public void actionPerformed(ActionEvent e) {

In this method you will write your test conditions to decide what color your buttons will change the panel.
To change a background color use the .setBackground() method. For example:

if (o.equals(redButton)) {
   colorPanel.setBackground(Color.RED);}

Does this make sense?

So I made these changes which I what I think you were saying...but when I run it and click the buttons the background doesn't change.

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

public class ColorViewer extends JFrame
{     
	public ColorViewer()  
	{        
		colorPanel = new JPanel();      
		add(colorPanel, BorderLayout.CENTER);   
		createControlPanel();    
		setSampleColor();     
		setSize(FRAME_WIDTH, FRAME_HEIGHT);  
	}   
	public void createControlPanel() 
	{     
		class ColorListener implements ActionListener     
		{          
			public void actionPerformed(ActionEvent e)        
			{           
				setSampleColor();  
			}     
		}        
		
		ActionListener listener = new ColorListener();       
		
		redButton = new JButton("Red");    
		redButton.addActionListener(listener);
		
		greenButton = new JButton("Green");     
		greenButton.addActionListener(listener);      
		
		blueButton = new JButton("Blue"); 
		blueButton.addActionListener(listener);     
		
		JPanel controlPanel = new JPanel();      
		controlPanel.setLayout(new GridLayout(3, 2));     
		
		controlPanel.add(new JLabel("Red"));    
		controlPanel.add(redButton); 
		
		controlPanel.add(new JLabel("Green"));  
		controlPanel.add(greenButton);   
		
		controlPanel.add(new JLabel("Blue"));  
		controlPanel.add(blueButton);      
		
		add(controlPanel, BorderLayout.SOUTH);   
		}    
	
	public void setSampleColor()  
	{        
		colorPanel.repaint();   
	}   
	public void actionPerformed(ActionEvent e)
	{	 
		Object o = e.getSource(); 	 
				
		if (o.equals(redButton)) 
		{  
			colorPanel.setBackground(Color.RED);
		}
		else if (o.equals(greenButton))
		{
			colorPanel.setBackground(Color.GREEN);
		}
		else
			colorPanel.setBackground(Color.BLUE);
	}   
	
	private JPanel colorPanel;  
	private JButton redButton;  
	private JButton greenButton; 
	private JButton blueButton;   
	private static final int FRAME_WIDTH = 300;  
	private static final int FRAME_HEIGHT = 400;
}

Nevermind...I moved the if statements section up in the code and now it's working...thanks everyone for you help.

I forgot to post this earlier after I saw you solved it, but here is how I modified your code, so you can see another way of doing it:

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

public class ColorViewer extends JFrame {
	/**
	 * 
	 */

	private JPanel colorPanel;
	private JButton redButton;
	private JButton greenButton;
	private JButton blueButton;

	private static final int FRAME_WIDTH = 300;
	private static final int FRAME_HEIGHT = 400;
	private static final long serialVersionUID = 1L;

	public ColorViewer() {
		colorPanel = new JPanel();
		add(colorPanel, BorderLayout.CENTER);
		redButton = new JButton("Red");
		greenButton = new JButton("Green");
		blueButton = new JButton("Blue");

		JPanel controlPanel = new JPanel();

		controlPanel.setLayout(new GridLayout(3, 2));
		controlPanel.add(new JLabel("Red"));
		controlPanel.add(redButton);
		controlPanel.add(new JLabel("Green"));
		controlPanel.add(greenButton);
		controlPanel.add(new JLabel("Blue"));
		controlPanel.add(blueButton);
		add(controlPanel, BorderLayout.SOUTH);

		ActionListener listener = new ColorListener();
		redButton.addActionListener(listener);
		greenButton.addActionListener(listener);
		blueButton.addActionListener(listener);

		setSize(FRAME_WIDTH, FRAME_HEIGHT);

	}

	private class ColorListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Object o = e.getSource();
			if (o.equals(redButton)) {
				colorPanel.setBackground(Color.RED);
			} else if (o.equals(greenButton)) {
				colorPanel.setBackground(Color.GREEN);
			} else if (o.equals(blueButton)) {
				colorPanel.setBackground(Color.BLUE);
			}
		}
	}

}

I see what you mean by writing it with less now. Thank you for showing me this alternate way. I have always been one to do everything the long way but like to look at how different codes can do the same thing. Thanks again for all your help.

Another alternate would be to drop the if() statements altogether and give the color information to your listener object when you create it:

private class ColorListener implements ActionListener {
        private Color color;

        public ColorListener(Color color) {
            this.color = color;
        }

        public void actionPerformed(ActionEvent e) {
            colorPanel.setBackground(color);
        }
    }

adding to the buttons like so

redButton.addActionListener(new ColorListener(Color.RED));
        greenButton.addActionListener(new ColorListener(Color.GREEN));
        blueButton.addActionListener(new ColorListener(Color.BLUE));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.