Reputation: 41
How would give different JButtons using the same ActionPerformed function different functionality?
public class ToolBarExample extends JPanel implements ActionListener {
public JTextPane pane;
public JMenuBar menuBar;
public JToolBar toolBar;
public JButton Aster;
public JButton Azaleas;
public JButton ChristFern;
public JButton JapBarberry;
public ToolBarExample() {
toolBar = new JToolBar("Formatting", JToolBar.VERTICAL);
this.Aster = new JButton(new ImageIcon("images/PlantIcons/[Blueprint]_Aster.png"));
this.toolBar.add(Aster);
this.Aster.addActionListener(this);
this.Azaleas = new JButton(new ImageIcon("images/PlantIcons/[Blueprint]_Azaleas.png"));
this.toolBar.add(Azaleas);
this.Azaleas.addActionListener(this);
this.ChristFern = new JButton(new ImageIcon("images/PlantIcons/[Blueprint]_ChristmasFern.png"));
this.toolBar.add(ChristFern);
this.ChristFern.addActionListener(this);
this.JapBarberry = new JButton(new ImageIcon("images/PlantIcons/[Blueprint]_JapaneseBarberry.png"));
this.toolBar.add(JapBarberry);
this.JapBarberry.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
try {
if(e.getSource() == Aster) {
NativePlant plant1 = new NativePlant(4, 5, 600, "test", "images/[Blueprint]_Aster.png", 200, 600);
Game.setNatives(plant1);
}
if(e.getSource() == Azaleas) {
NativePlant plant1 = new NativePlant(4, 5, 600, "test", "images/[Blueprint]_Azaleas.png", 100, 600);
Game.setNatives(plant1);
}
if(e.getSource() == ChristFern) {
NativePlant plant1 = new NativePlant(4, 5, 600, "test", "images/[Blueprint]_ChristmasFern.png", 300, 600);
Game.setNatives(plant1);
}
if(e.getSource() == JapBarberry) {
NativePlant plant1 = new NativePlant(4, 5, 600, "test", "images/[Blueprint]_JapaneseBarberry.png", 400, 600);
Game.setNatives(plant1);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Right now I am trying to use the e.getSource to find out what button is being pushed, and then execute something different for each button. However, right now the only button that is working is the first one (Aster).
Does anyone know what I am doing wrong, or what I should be doing?
Upvotes: 2
Views: 280
Reputation: 77
instead of having it be
e.getSource() == Aster
change it to:
e.getSource().equals(Aster)
it looks like the same thing, but it will give you the results your looking for... because .equals() is checking for the state of an object, where as '==' is checking the actually instances.
http://www.javabeat.net/qna/13-what-is-difference-between-equals-and-/
Upvotes: 0
Reputation: 2297
You can use the setActionCommand() of each button and call the respective getActionCommand from the actionPerformed. Using constants to define the actions would be a good approach in my opinion. A small sample as follows;
public class SwingTest extends JFrame implements ActionListener{
public SwingTest()
{
JButton btnOne = new JButton("test one");
btnOne.addActionListener(this);
btnOne.setActionCommand("TestOne");
JButton btnTwo = new JButton("test two");
btnTwo.addActionListener(this);
btnTwo.setActionCommand("TestTwo");
this.setLayout(new FlowLayout());
this.getContentPane().add(btnOne);
this.getContentPane().add(btnTwo);
}
public static void main(String[] args) {
SwingTest t = new SwingTest();
t.pack();
t.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}
Upvotes: 1