//this is the array wherein colors are in dropbox type....
private final String[] Options = new String[] {"Select Color", "RED" , "YELLOW", "BLUE" , "GREEN"};
private final JComboBox cmb1 = new JComboBox (Options);
//this is the switch case.. if I choose red, the whole frame will be in red. enter image description here
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// Get the selected value on the JCOmboBox
String SelectedValue = cmb1.getSelectedItem().toString();
// Display
switch (SelectedValue){
//if i choose red, the text will display in the frame below. How can I make the frame in the "red" turns red??
case "RED" :
txt1.setText ("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon"); break;
case "YELLOW" :
txt1.setText ("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron"); break;
case "BLUE" :
txt1.setText ("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire"); break;
case "GREEN" :
txt1.setText ("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive"); break;
default:
txt1.setText("");
}
}
}
This is what I've done... want to insert colors..
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ComboBoxColor extends JFrame {
// Declare Combo Box with Values from Array
private final String[] Options = new String[] { "Select Color", "RED",
"YELLOW", "BLUE", "GREEN" };
private final JComboBox cmb1 = new JComboBox(Options);
public void itemStateChanged(java.awt.event.ItemEvent evt) {
itemStateChanged(evt);
}
// Declare JTextArea and wrap it in a JScrollPane
private final JTextArea txt1 = new JTextArea(2, 20);
private final JScrollPane scr = new JScrollPane(txt1);
// ActionListener
private final ButtonHandler BH = new ButtonHandler();
public JComboBox combo;
// Constructor
public ComboBoxColor() {
// Add ActionListener
cmb1.addActionListener(BH);
// Set Layout
Container pane = this.getContentPane();
pane.setLayout(new GridLayout(2, 4));
// Add Components
pane.add(cmb1);
pane.add(scr);
// Set JFrame Properties
this.setTitle("COMBO BOX COLOR");
this.setSize(500, 150);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
// ActionListener Sub-Class
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// Get the selected value on the JCOmboBox
String SelectedValue = cmb1.getSelectedItem().toString();
// Display
switch (SelectedValue) {
case "RED":
txt1.setText("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon");
break;
case "YELLOW":
txt1.setText("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron");
break;
case "BLUE":
txt1.setText("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire");
break;
case "GREEN":
txt1.setText("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive");
break;
default:
txt1.setText("");
}
}
}
// Main Method
public static void main(String[] args) {
ComboBoxColor CC = new ComboBoxColor();
}
}