Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to be able to change the background color using radio buttons in the

ID: 3681162 • Letter: I

Question

I'm trying to be able to change the background color using radio buttons in the following java code. The light radio button should change the background color to white, and dark should change the background color to black. There are two different panels. The top one containing the buttons and the label shouldn't have the background change. The other panel that includes the image is the one that needs to have the background changed when selecting a radio button. Here is the code below.

import javax.swing.border.EmptyBorder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class GuiExercise extends JFrame implements ActionListener
{

   private JPanel contentPane;
   private JRadioButton white;
   private JRadioButton black;
  
   JPanel bkground = new JPanel();

  
   public GuiExercise()
   {
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setBounds(100, 100, 430, 530);
       contentPane = new JPanel();
       contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
       setContentPane(contentPane);
       contentPane.setLayout(null);
      
      
       JPanel panel = new JPanel();
       panel.setBackground(Color.WHITE);
       panel.setBounds(0, 0, 412, 58);
       contentPane.add(panel);
       panel.setLayout(null);
      
      
       JRadioButton white = new JRadioButton("light");
       white.setSelected(true);
       white.setContentAreaFilled(false);
       white.setBounds(8, 19, 61, 25);
       panel.add(white);
      
      
       JRadioButton black = new JRadioButton("dark");
       black.setContentAreaFilled(false);
       black.setBounds(343, 19, 61, 25);
       panel.add(black);
  
      
       JLabel lblPurpleCloud = new JLabel("Purple Cloud");
       lblPurpleCloud.setFont(new Font("Tahoma", Font.PLAIN, 28));
       lblPurpleCloud.setBounds(123, 13, 166, 39);
       panel.add(lblPurpleCloud);
      
      
       bkground.setBackground(Color.WHITE);
       bkground.setBounds(0, 60, 412, 423);
       contentPane.add(bkground);
       bkground.setLayout(null);
      
      
       JLabel label = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("S.jpg")));
       label.setBounds(78, 78, 250, 250);
       bkground.add(label);
      
      
       ButtonGroup group=new ButtonGroup();
       group.add(white);
       group.add(black);
      
       white.addActionListener(null);
       black.addActionListener(null);
      
   }

   public void actionPerformed(ActionEvent e)
   {
       if(e.getSource() == white)
       {
           bkground.setBackground(Color.WHITE);
       }
       if(e.getSource() == black)
       {
           bkground.setBackground(Color.BLACK);
       }

      
   }
}

dark tPurple Cloud light

Explanation / Answer

Hi, Please find th below updated program. Its working fine now.

GuiExercise.java

import javax.swing.border.EmptyBorder;

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

public class GuiExercise extends JFrame implements ActionListener
{
private JPanel contentPane;
private JRadioButton white;
private JRadioButton black;
JPanel bkground = new JPanel();

public GuiExercise()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 430, 530);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);


JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setBounds(0, 0, 412, 58);
contentPane.add(panel);
panel.setLayout(null);


white = new JRadioButton("light");
white.setSelected(true);
white.setContentAreaFilled(false);
white.setBounds(8, 19, 61, 25);
panel.add(white);


black = new JRadioButton("dark");
black.setContentAreaFilled(false);
black.setBounds(343, 19, 61, 25);
panel.add(black);


JLabel lblPurpleCloud = new JLabel("Purple Cloud");
lblPurpleCloud.setFont(new Font("Tahoma", Font.PLAIN, 28));
lblPurpleCloud.setBounds(123, 13, 166, 39);
panel.add(lblPurpleCloud);


bkground.setBackground(Color.WHITE);
bkground.setBounds(0, 60, 412, 423);
contentPane.add(bkground);
bkground.setLayout(null);


JLabel label = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("S.jpg")));
label.setBounds(78, 78, 250, 250);
bkground.add(label);


ButtonGroup group=new ButtonGroup();
group.add(white);
group.add(black);

white.addActionListener(this);
black.addActionListener(this);
   this.setVisible(true);

}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == white)
{
bkground.setBackground(Color.WHITE);
}
if(e.getSource() == black)
{
bkground.setBackground(Color.BLACK);
}

}

public static void main(String arg[]){
new GuiExercise();
}
}

Issues with your code is:

There are two issues.

1) Since you declared global JRadioButton objects, you should not declare the same local objects.

private JRadioButton white;
private JRadioButton black;

Wrong:

JRadioButton white = new JRadioButton("light");

JRadioButton black = new JRadioButton("dark");

Correct:

white = new JRadioButton("light");

black = new JRadioButton("dark");

I used only global variable. removed local declaration.

2) Whle you are registering Action Listener you should pass the component object. In this case you have passed the null value as an event object. You should pass the event object.

Wrong:

white.addActionListener(null);
black.addActionListener(null);

Correct:

white.addActionListener(null);
black.addActionListener(null);