Using swing components in a handler
I have my code here:
Code:
SelectListener handler = new SelectListener ();
final JComboBox SelectColor = new JComboBox(action.getColors());
SelectColor.setSelectedIndex(0);
SelectColor.addActionListener(handler);
this.add(SelectColor);
and then my handler here
Code:
private class SelectListener implements ActionListener {
Actions action = new Actions();
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == "Submit"){
action.ChangeColor(SelectWord.getSelectedIndex(), SelectColor.getSelectedIndex());
}
}
}
When I try to use SelectColor or Select Word it tells me it can not be resolved. But I already linked them to the handler. Can someone tell me what I am doing wrong?
Thanks in advance!
Re: Using swing components in a handler
It's likely a scope issue -- the variables you are trying to use are not visible in the scope of where you are trying to use them.
To give more specific help, you'll need to post more code. Also, please read up on and abide by the Java naming conventions since if you don't, it makes it difficult for others including the volunteers in this forum to be able to read and understand your code and help you.
Re: Using swing components in a handler
Sorry about the naming, Ill read up on that. Anyway here is the entire code for GUI.java
Code:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GUI extends JFrame {
public GUI (WordManager manager, Actions action){
super ("Algorithm Analyzer 0.1");
this.setLayout(new FlowLayout());
for (int i = 0; i < manager.getWords().length; i++){
this.add(manager.getWords()[i].getLabel());
}
SelectListener handler = new SelectListener ();
final JComboBox SelectWord = new JComboBox(manager.getWords());
SelectWord.setSelectedIndex(0);
SelectWord.addActionListener(handler);
this.add(SelectWord);
final JComboBox SelectColor = new JComboBox(action.getColors());
SelectColor.setSelectedIndex(0);
SelectColor.addActionListener(handler);
this.add(SelectColor);
JButton Button = new JButton ("Submit");
Button.addActionListener(handler);
this.add(Button);
}
private class SelectListener implements ActionListener {
Actions action = new Actions();
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == "Submit"){
action.ChangeColor(SelectWord.getSelectedIndex(), SelectColor.getSelectedIndex());
}
}
}
}
Re: Using swing components in a handler
Yep, it's just as I suspected -- a scope issue. Both SelectWord and SelectColor are declared inside of the GUI constructor and thus are only visible within the constructor. If you need to use them elsewhere in the class, you'll need to declare them at the class level.
And again, please read up on Java naming conventions. Variable and method names should start with a lower-case letter, and class names should begin with an upper-case letter. If you comply with these conventions, others will understand your code much quicker and more fully.
Re: Using swing components in a handler
Thank you so much for your help!
Unfortunately I must make another inquiry here.
I have my event handler like this:
Code:
private class SelectListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == "Submit"){
/* Method 1*/
action.ChangeColor(manager.getWords()[SelectWord.getSelectedIndex()],
action.getColors()[SelectColor.getSelectedIndex()]);
}
}
}
Now its not giving any errors (which is awesome!), however what I want to do is have it change the color of a JLabel at runtime. Currently method 1 takes a word object (which has a JLabel, a color and then changes the JLabel's foreground color. The code looks like so
Code:
public class Actions {
private final Color Colors[] = {Color.BLACK,Color.BLUE,Color.CYAN,Color.DARK_GRAY,Color.GRAY,
Color.GREEN,Color.LIGHT_GRAY,Color.MAGENTA,Color.ORANGE,
Color.PINK,Color.RED,Color.WHITE,Color.YELLOW};
private String[] ColorNames = {"Black","Blue","Cyan","Dark Gray","Gray","Green","Light Gray","Magenta",
"Orange","Pink","Red","White","Yellow"};
/* Method 2 */
public Word ChangeColor (Word word, Color color){
word.getLabel().setForeground(color);
return word;
}
public Color[] getColors (){
return Colors;
}
public String[] getColorNames(){
return ColorNames;
}
}
Now, I am unsure as how to get the JLabel that is returned from ChangeColor to replace the correct existing JLabel at runtime. Can anyone point me in the right direction here?
Thanks
Note: I did a bit of reading on the naming conventions in java, when I get this part of the program to work Ill go back and rename things/add comments.
Re: Using swing components in a handler
Any ideas? (Sorry for bump, Id rather not start a new thread...)
Re: Using swing components in a handler
Let me get this straight -- are you trying to swap JLabels at run-time? Why? Why not simply change the displayed JLabel's text and/or icon?