So I'm working on a program to change the Color of a Panel with a Combo Box.
So far I have the Panel and the ComboBox with th options. However, when you click the option nothing happens. This is my first GUI program and I'm not too familiar with how to carry out actions but here's what I have so far. I'm just looking to get pointed in the right direction.
import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JComboBox; public class ThreeColorsFrame extends JFrame { private JPanel ColorPanel; private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 300; private JComboBox facenameCombo; private ActionListener listener; public ThreeColorsFrame() { setSize(FRAME_WIDTH, FRAME_HEIGHT); ColorPanel = new JPanel(); add(ColorPanel, BorderLayout.CENTER); createComboBox(); } public void createComboBox() { JPanel northPanel = new JPanel(); facenameCombo = new JComboBox(); facenameCombo.addItem(Color.RED); facenameCombo.addItem(Color.GREEN); facenameCombo.addItem(Color.BLUE); facenameCombo.setEditable(true); facenameCombo.addActionListener(listener); setColor(); northPanel.add(facenameCombo); add(northPanel, BorderLayout.NORTH); } public void setColor() { // Get font name Color color = (Color) facenameCombo.getSelectedItem(); /* if (color.equals("Red")) {Color thecolor = Color.RED; ColorPanel.setBackground(thecolor);} if (color.equals("Green")) {Color thecolor = Color.GREEN; ColorPanel.setBackground(thecolor);} if (color.equals("Blue")) {Color thecolor = Color.BLUE; ColorPanel.setBackground(thecolor);} // Set font of text field */ ColorPanel.setBackground(color);} }