I think you cannot do this however you want. A possible approach is to use Action Commands as described in this.
JButton hello = new JButton("Hello"); hello.setActionCommand(Actions.HELLO.name()); hello.addActionListener(instance); frame.add(hello); JButton goodbye = new JButton("Goodbye"); goodbye.setActionCommand(Actions.GOODBYE.name()); goodbye.addActionListener(instance); frame.add(goodbye); ... } @Override public void actionPerformed(ActionEvent evt) { if (evt.getActionCommand() == Actions.HELLO.name()) { JOptionPane.showMessageDialog(null, "Hello"); } else if (evt.getActionCommand() == Actions.GOODBYE.name()) { JOptionPane.showMessageDialog(null, "Goodbye"); } }
This is just an example, but you get the idea.
Asier aranbarri
source share