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

    Default Correctly passing an element to a method

    I am trying to change the color of a JPanel based on user input.

    Currently my "submit" button's event handling code looks like this:

    Java Code:
    private class SelectListener implements  ActionListener  {
    		
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			// TODO Auto-generated method stub
    			if (e.getSource() == "Submit"){
    				
    				action.ChangeColor(manager.getWords()[SelectWord.getSelectedIndex()],
    						action.getColors()[SelectColor.getSelectedIndex()]);
    
    				
    			}
    			
    		}
    	}
    
    }
    method change color looks like this:

    Java Code:
    	public Word ChangeColor (Word word, Color color){
    		
    		word.getLabel().setForeground(color);
    		
    		return word;
    	}
    the word class is like this:

    Java Code:
    package AlgorithmAnalyzer;
    
    import java.awt.Color;
    import java.awt.Font;
    
    import javax.swing.JLabel;
    
    public class Word {
    	
    	private JLabel wordLabel;
    	private Color defaultColor = Color.BLACK;
    	private String Name;
    	private Font defaultFont = new Font("Serif",Font.BOLD,24);
    	
    	
    	public Word (String text, WordManager manager) {
    		
    		Name = text;
    		wordLabel = new JLabel(text);
    		wordLabel.setFont(defaultFont);
    		wordLabel.setForeground(defaultColor);
    		
    		manager.addWord(this);
    		
    	}
    	
    	public JLabel getLabel (){
    		
    		return wordLabel;
    	}
    	
    	public String getName(){
    		return Name;
    	}
    	
    	
    
    }

    Now, my logic here is that Im passing a word object to changeColor, which gets the words JPanel and changes its color. After that it returns a word (I'm not sure if this is required or not). When I run the program however nothing happens. I was guessing that Im passing either the wrong thing to my method or my method isn't changing the right thing, however trying to fix the problem assuming I was guessing correctly didnt help things. So I turn to the community :)

    How do I get that color update to work?

    Thanks in advance!

    PS: I have been informed on a previous thread that my naming of classes,variables,methods,etc. is unorthodox, I have not yet gotten a chance to fix all the code to a standard java naming scheme.

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

    Default Re: Correctly passing an element to a method

    Regarding
    Java Code:
    public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if (e.getSource() == "Submit"){
    You shouldn't compare Strings with == but rather with the equals method.

    But regardless of this, ActionEvent#getSource() returns the object that initiated the event, in this situation, the JButton, and certainly never a String.

    ActionEvent#getActionCommand() on the other hand does return the actionCommand String, often the text of the button. If you do wish to test for this, though again use the equals method.

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

    Default Re: Correctly passing an element to a method

    Thanks, the button works now :) I'm getting a null pointer but I already have that pinned down and know how to fix it :)

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

    Default Re: Correctly passing an element to a method

    Quote Originally Posted by aianta View Post
    Thanks, the button works now :) I'm getting a null pointer but I already have that pinned down and know how to fix it :)
    Good show, and good luck with the rest of the project!

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

    Default Re: Correctly passing an element to a method

    So I fixed that null pointer that I was getting (was trying to access a color that didn't exist), now however I'm getting another null pointer and this one is stumping me. I'm calling getSelectedIndex and getting a null pointer when it uses the value to find an item off an array. I replaced the getSelectedIndex with the value it should have returned and found that to work fine.

    Then I created a test variable and assigned the getSelectedIndex to it so I can check what value its returning. However, it turns out that it never returns a value at all. I checked my variable declarations and everything seemed fine there. Any ideas as to whats causing this error:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at AlgorithmAnalyzer.GUI$SelectListener.actionPerformed(GUI.java:60)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    heres all the gui.java code:
    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 {
    	
    	private JComboBox SelectColor;
    	private JComboBox SelectWord;
    	private JButton Button;
    	private WordManager manager;
    	
    	Actions action = new Actions();
    	
    	public GUI (WordManager manager){
    		
    		super ("test");
    		this.setLayout(new FlowLayout());
    		
    		this.manager = manager;
    		
    		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.getWordNames());
    		SelectWord.setSelectedIndex(0);
    		SelectWord.addActionListener(handler);
    		this.add(SelectWord);
    		
    		
    		final JComboBox SelectColor = new JComboBox(action.getColorNames());
    		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  {
    		
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			// TODO Auto-generated method stub
    			if (e.getActionCommand() == "Submit"){
    				
    				int test = SelectWord.getSelectedIndex();
    				
    				System.out.println(test);
    				
    				action.ChangeColor(manager.getWords()[SelectWord.getSelectedIndex()],
    						action.getColors()[SelectColor.getSelectedIndex()]);
    
    				
    			}
    			
    		}
    	}
    
    }
    Last edited by aianta; 10-28-2011 at 06:50 PM.

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

    Default Re: Correctly passing an element to a method

    Any ideas :( ?

Similar Threads

  1. How to correctly overide hashcode method in Java
    By javin.paul in forum Reviews / Advertising
    Replies: 0
    Last Post: 10-08-2011, 09:57 AM
  2. drawString() method not working correctly
    By tnixon22 in forum New To Java
    Replies: 2
    Last Post: 02-25-2011, 09:57 PM
  3. Replies: 5
    Last Post: 10-30-2010, 01:05 AM
  4. Replies: 1
    Last Post: 02-08-2010, 02:33 PM
  5. My rotate 2d pos method isnt working correctly..
    By Addez in forum New To Java
    Replies: 5
    Last Post: 12-01-2009, 10:04 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
  •