Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Using swing components in a handler

  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Using swing components in a handler

    I have my code here:

    Java 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

    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());
    			}
    			
    		}
    	}
    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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.
    DarrylBurke likes this.

  3. #3
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default 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());
    			}
    			
    		}
    	}
    
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default 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:
    Java 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

    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;
    	}
    }
    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.

  6. #6
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Re: Using swing components in a handler

    Any ideas? (Sorry for bump, Id rather not start a new thread...)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. binding swing components
    By furqankhan in forum Advanced Java
    Replies: 1
    Last Post: 06-25-2010, 01:08 PM
  2. Swing components not working
    By primalpop in forum AWT / Swing
    Replies: 30
    Last Post: 11-15-2009, 06:10 PM
  3. add remove swing components
    By willemjav in forum New To Java
    Replies: 6
    Last Post: 10-09-2008, 08:37 PM
  4. Where is it best to declare swing components?
    By MacNstuff in forum AWT / Swing
    Replies: 1
    Last Post: 02-06-2008, 01:59 AM
  5. HTML on Swing Components
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •