Reputation: 1
I'm new to java GUI programming and while working on the project I'm getting the error cannot find symbol on my addActionListener for my JRadioButtons, I'm not quite sure what I'm doing wrong since I didn't receive the same error when working with JButtons.
Here's my code:
public void SouthPanel() {
JRadioButton greenButton = new JRadioButton("Green");
JRadioButton blueButton = new JRadioButton("Blue");
JRadioButton cyanButton = new JRadioButton("Cyan");
ButtonGroup group = new ButtonGroup();
group.add(greenButton);
group.add(blueButton);
group.add(cyanButton);
greenButton.addActionListener(new RadioButtonListener());
blueButton.addActionListener(new RadioButtonListener());
cyanButton.addActionListener(new RadioButtonListener());
SouthPanel = new JPanel();
add(greenButton);
add(blueButton);
add(cyanButton);
add(SouthPanel);
setVisible(true);
}
private class RadioButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String actionRadio = e.getActionCommand();
if (actionRadio.equals("Green")) {
label.setForeground(Color.GREEN);
}
else if (actionRadio.equals("Blue")) {
label.setForeground(Color.BLUE);
}
else if (actionRadio.equals("Cyan")) {
label.setForeground(Color.CYAN);
}
}
Upvotes: 0
Views: 421
Reputation: 270
See the complete code here.
class SouthPanel extends JPanel {
JLabel label = new JLabel("label");
public SouthPanel() {
JRadioButton greenButton = new JRadioButton("Green");
JRadioButton blueButton = new JRadioButton("Blue");
JRadioButton cyanButton = new JRadioButton("Cyan");
ButtonGroup group = new ButtonGroup();
group.add(greenButton);
group.add(blueButton);
group.add(cyanButton);
greenButton.addActionListener((ActionListener) new
RadioButtonListener());
blueButton.addActionListener(new RadioButtonListener());
cyanButton.addActionListener(new RadioButtonListener());
JPanel SouthPanel = new JPanel();
add(label);
add(greenButton);
add(blueButton);
add(cyanButton);
add(SouthPanel);
setVisible(true);
JFrame frame = new JFrame();
frame.setContentPane(this);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new SouthPanel();
}
private class RadioButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String actionRadio = e.getActionCommand();
if (actionRadio.equals("Green")) {
label.setForeground(Color.GREEN);
} else if (actionRadio.equals("Blue")) {
label.setForeground(Color.BLUE);
} else if (actionRadio.equals("Cyan")) {
label.setForeground(Color.CYAN);
}
}
}
}
Upvotes: 0
Reputation: 430
You have created an instance of the JPanel and inserted into SouthPanel class. How can it be done.
SouthPanel = new JPanel();
add(greenButton);
add(blueButton);
add(cyanButton);
add(SouthPanel);
setVisible(true);
Into where are you adding the buttons and adding the SouthPanel.! Please check this one.Seems the error is from here.3 buttons are added,for 3 times and error is shown for 3 times.right.Seems error is from here. Check here.
Upvotes: 0
Reputation: 492
To use getActionCommand()
on a Button
or RadioButton
, you should have previously set the ActionCommand
button.setActionCommand(String val);
You'd be able to get it back when you make a call to:
button.getActionCommand(); //This would return the string you previously set.
For a TextField, ActionCommand would give you the text in the TextField by default if you do not set it.
That's where you are probably missing the line.
Upvotes: 0
Reputation: 195
To my knowledge, the error "cannot find symbol" usually refers to a variable that can't be resolved by the compiler.
On what line does the error occur?
What seems a bit odd at first glance is following statement:
SouthPanel = new JPanel();
and add(SouthPanel);
since SouthPanel is the name of your method and you didn't give a name to your SouthPanel (?) object.
Upvotes: 1