Changing Panel Color with a ComboBox
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.
Code java:
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);}
}
Re: Changing Panel Color with a ComboBox
The code you posted does not appear to implement a listener. It defines a listener, but never implements the actionPerformed method, which is required. Recommend you read through the following links:
How to Use Combo Boxes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
Basically, you need to instantiate the listener variable, which the code below will do but you must complete the code for the results you want
Code Jav:
listener = new ActionListener(){
public void actionPerformed(ActionEvent e){
//place code here
}
};
Re: Changing Panel Color with a ComboBox
So I'm trying to get the action listener right but I get a ; Expected error at the second to last }. What am I doing wrong?
Code java:
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);
northPanel.add(facenameCombo);
add(northPanel, BorderLayout.NORTH);
listener = new ActionListener(){
public void actionPerformed(ActionEvent e){
facenameCombo = (JComboBox)e.getSource();
Color color = (Color) facenameCombo.getSelectedItem();
ColorPanel.setBackground(color);}
}
}
Re: Changing Panel Color with a ComboBox
You need a semicolon at the end of the anonymous class definition (my code above did not reflect this - edited)
Re: Changing Panel Color with a ComboBox
Lol so I have no errors now. The only problem is nothing happens when I Choose an option form the Combo Box nothing happens :(
Code java:
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);
northPanel.add(facenameCombo);
add(northPanel, BorderLayout.NORTH);
listener = new ActionListener(){
public void actionPerformed(ActionEvent e){
facenameCombo = (JComboBox)e.getSource();
Color color = (Color) facenameCombo.getSelectedItem();
ColorPanel.setBackground(color);}
};
}
}
I draw the box, give it an action listener, and an action to perform, and I missing something?
Re: Changing Panel Color with a ComboBox
Add some println's in there to see the values of the variables...in particular, the listener object when you try to add the listener will be null. You must first instantiate it (create the anonymous class) and then add it as the listener to the JComboBox (in its current for it is being done the other way around).
Re: Changing Panel Color with a ComboBox
How do you instantiate an anonymous class?
Re: Changing Panel Color with a ComboBox
Quote:
Originally Posted by
bengregg
How do you instantiate an anonymous class?
You did already by implementing the action listener. Google the term anonymous class and you will see many explanations. My recommendation above might be better illustrated with the following:
Code :
String myString = null;
System.out.println(myString);
myString = "Testing";
Println will print null. The same is the case with the listener variable in your code listing - you want to assign it to something before trying to use it