Reputation: 15
My program is trying to save the state of a ButtonGroup, so that it can be written to a file at the end of the program, or restored to the buttons if the user selects to go 'Back'.
I have previously found this question:
How do I get which JRadioButton is selected from a ButtonGroup
However my code below errors upon compiling as the ButtonGroup is not recognised when being used within the ActionPerformed method.
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Enumeration;
public class GraphicalInterface implements ActionListener
{
private static String[] tweetList =
{"/Users/Chris/Desktop/Code/Tweets/One.jpg", "/Users/Chris/Desktop/Code/Tweets/Two.jpg", "/Users/Chris/Desktop/Code/Tweets/Three.jpg",
"/Users/Chris/Desktop/Code/Tweets/Four.jpg", "/Users/Chris/Desktop/Code/Tweets/Five.jpg", "/Users/Chris/Desktop/Code/Tweets/Six.jpg",
"/Users/Chris/Desktop/Code/Tweets/Seven.jpg", "/Users/Chris/Desktop/Code/Tweets/Eight.jpg"};
private int[] answers = {4, 4, 4, 4, 4, 4, 4, 4};
private int currentTweet = 0;
JFrame surveyFrame = new JFrame("User Survey");
JPanel surveyPanel = new JPanel(new FlowLayout());
JButton buttonBack = new JButton("Back");
JButton buttonNext = new JButton("Next");
JButton buttonFinish = new JButton("Finish");
JRadioButton cred1 = new JRadioButton("Extrememly Credible");
JRadioButton cred2 = new JRadioButton("Credible");
JRadioButton cred3 = new JRadioButton("Somewhat Credible");
JRadioButton cred4 = new JRadioButton("Undecided");
JRadioButton cred5 = new JRadioButton("Somewhat Incredible");
JRadioButton cred6 = new JRadioButton("Incredible");
JRadioButton cred7 = new JRadioButton("Extrememly Incredible");
JLabel tweetLabel = new JLabel(new ImageIcon(tweetList[currentTweet]));
public void Interface()
{
surveyFrame.setVisible(true);
surveyFrame.setSize(500, 350);
surveyFrame.add(surveyPanel);
surveyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
surveyPanel.add(tweetLabel);
surveyPanel.add(buttonNext);
surveyPanel.add(buttonBack);
surveyPanel.add(buttonFinish);
surveyPanel.add(cred1);
surveyPanel.add(cred2);
surveyPanel.add(cred3);
surveyPanel.add(cred4);
surveyPanel.add(cred5);
surveyPanel.add(cred6);
surveyPanel.add(cred7);
cred4.setSelected(true);
ButtonGroup tweetCredibility = new ButtonGroup();
tweetCredibility.add(cred1);
tweetCredibility.add(cred2);
tweetCredibility.add(cred3);
tweetCredibility.add(cred4);
tweetCredibility.add(cred5);
tweetCredibility.add(cred6);
tweetCredibility.add(cred7);
buttonNext.addActionListener(this);
buttonBack.addActionListener(this);
buttonFinish.addActionListener(this);
}
public void actionPerformed(ActionEvent input)
{
Object source = input.getSource();
if (source == buttonNext)
{
if (currentTweet < tweetList.length-1)
{
answers[currentTweet] = getCredRating(tweetCredibility);
currentTweet++;
ImageIcon nextTweet = new ImageIcon(tweetList[currentTweet]);
tweetLabel.setIcon(nextTweet);
// restore button from next image
}
}
if (source == buttonBack)
{
if (currentTweet > 0)
{
answers[currentTweet] = getCredRating(tweetCredibility);
currentTweet--;
ImageIcon nextTweet = new ImageIcon(tweetList[currentTweet]);
tweetLabel.setIcon(nextTweet);
// restore button from previous image
}
}
if (source == buttonFinish)
{
// save answers to file
}
}
public int getCredRating(ButtonGroup input)
{
int n = 1;
for (Enumeration<AbstractButton> buttons = input.getElements(); buttons.hasMoreElements(); n++)
{
AbstractButton button = buttons.nextElement();
if (button.isSelected())
{
return n;
}
}
}
}
Complier: Cannot find variable tweetCredibility
Any ideas why this is happening?
Thanks.
Upvotes: 1
Views: 819
Reputation: 324098
ButtonGroup tweetCredibility = new ButtonGroup();
You are defining the ButtonGroup as a local variable. That is only the GraphicalInterface() constructor knows about it.
You need to define the button group as an instance variable so it can be used by any method of the class. So define the ButtonGroup where you define the JRadioButtons.
Also, the surveyFrame.setVisibe(true)
statement should be invoked as the last statement of the constructor AFTER all components have been added to the frame.
Upvotes: 1