Results 1 to 7 of 7
- 10-16-2011, 02:02 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
Using swing components in a handler
I have my code here:
and then my handler hereJava Code:SelectListener handler = new SelectListener (); final JComboBox SelectColor = new JComboBox(action.getColors()); SelectColor.setSelectedIndex(0); SelectColor.addActionListener(handler); this.add(SelectColor);
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?Java 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()); } } }
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.
- 10-16-2011, 04:52 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
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
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.
- 10-16-2011, 05:06 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
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:
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 soJava 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, 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?Java 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; } }
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.
- 10-19-2011, 02:59 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
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?
Similar Threads
-
binding swing components
By furqankhan in forum Advanced JavaReplies: 1Last Post: 06-25-2010, 12:08 PM -
Swing components not working
By primalpop in forum AWT / SwingReplies: 30Last Post: 11-15-2009, 05:10 PM -
add remove swing components
By willemjav in forum New To JavaReplies: 6Last Post: 10-09-2008, 07:37 PM -
Where is it best to declare swing components?
By MacNstuff in forum AWT / SwingReplies: 1Last Post: 02-06-2008, 12:59 AM -
HTML on Swing Components
By Java Tip in forum Java TipReplies: 0Last Post: 11-27-2007, 09:51 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks