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:
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:
Code:
public Word ChangeColor (Word word, Color color){
word.getLabel().setForeground(color);
return word;
}
the word class is like this:
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.
Re: Correctly passing an element to a method
Regarding
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.
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 :)
Re: Correctly passing an element to a method
Quote:
Originally Posted by
aianta
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!
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:
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:
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()]);
}
}
}
}
Re: Correctly passing an element to a method