I am a student of very basic Java. We did an assignment to make the background color change according to the radio button selected using several if statements. That worked fine. I decided to change the selection process to a combobox and use a switch case. It seems to me the process is failing the if statement in the switch case method. I'm trying to get a better understanding of how things work. The code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class Lab17_4combo extends JFrame implements ActionListener
{
Container container;
JComboBox colors;
public Lab17_4combo()
{
super("ComboBox ");
container = this.getContentPane();
container.setLayout(new FlowLayout());
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] selectColor = {"Red", "Yellow", "Blue", "Green", "Magenta"};
JComboBox colors = new JComboBox(selectColor);
colors.setSelectedIndex(-1);
colors.addActionListener(this);
container.add(colors);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int chgColor;
if(e.getSource() == colors)
{
chgColor = colors.getSelectedIndex();
switch(chgColor)
{
case 0:
container.setBackground(Color.red);
case 1:
container.setBackground(Color.yellow);
case 2:
container.setBackground(Color.blue);
case 3:
container.setBackground(Color.green);
case 4:
container.setBackground(Color.magenta);
}
}else
{
container.setBackground(Color.magenta);
}
}
public static void main(String[] args)
{
Lab17_4combo s = new Lab17_4combo();
}
}
I put in the else to check if it was failing the if. I'm assuming that is where the problem is, but I don't know how to fix it. Any help would be greatly appreciated. The original assignment has been completed, this is my own experimentation. I'm not asking for anyone to do my homework for me. Cheers
EDIT-- I have made the suggested changes to the code (Thanks to all for the suggestions). The background color of the container still does not change regardless of the selection I make from the combobox. I'm assuming there are mistakes elsewhere in the code, but I'm at a loss to find them. My expectation is that the background color of the container will change according to the selection I make from the combobox. This is not happening.
The revised code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class Lab17_4combo extends JFrame implements ActionListener
{
Container container;
JComboBox colors;
public Lab17_4combo()
{
super("ComboBox ");
container = this.getContentPane();
container.setLayout(new FlowLayout());
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] selectColor = {"Red", "Yellow", "Blue", "Green", "Magenta"};
JComboBox colors = new JComboBox(selectColor);
colors.setSelectedIndex(-1);
colors.addActionListener(this);
container.add(colors);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int chgColor;
if(e.getSource() == colors)
{
chgColor = colors.getSelectedIndex();
switch(chgColor)
{
case 0:
container.setBackground(Color.red);
break;
case 1:
container.setBackground(Color.yellow);
break;
case 2:
container.setBackground(Color.blue);
break;
case 3:
container.setBackground(Color.green);
break;
case 4:
container.setBackground(Color.magenta);
break;
}
}
}
public static void main(String[] args)
{
Lab17_4combo s = new Lab17_4combo();
}
}
With my limited knowledge of Java I'm not able to see where the mistake(s) may be. Any help would be appreciated.Cheers
breakafter eachcaseinswitchstatement.