Get button name from ActionListener?

I browsed the internet but cannot find the answer to this question.

I use a for loop to create 36 buttons named a1 a2, etc. and assigning each of them a unique Action command at the same time.

Later I want to get the button name from the actionPerformed (ActionEvent e) method.

I can get ActionCommand quite easily, but I also need a button name.

Any help is much appreciated!

Edit:

Here is the code I'm using:

String letters[] = {"0", "a", "b", "c", "d", "e", "f"}; JButton btn[] = new JButton[35]; int count = 0; for (int f=1; f < 7;f++){ for (int i=1; i < 7;i++){ btn[i] = new JButton(letters[f]+i, cup); System.out.println(btn[i])); mainGameWindow.add(btn[i]); btn[i].addActionListener(this); String StringCommand = Integer.toString(randomArrayNum()); btn[i].setActionCommand(StringCommand); count++; if(count == 18){ generateArray(); } } } 

This gives you 36 buttons for a 6x6 grid that go a1-6, b1-6, c1-6, etc.

I simply cannot control the buttons, as soon as I create them in this way, I cannot assign icons or get the name of the button.

Thanks at Advance

+4
source share
7 answers

Save the button link in Map

 String letters[] = {"0", "a", "b", "c", "d", "e", "f"}; JButton btn; int count = 0; HashMap<String,JButton> buttonCache = new HashMap<String,JButton>(); for (int f=1; f < 7;f++){ for (int i=1; i < 7;i++){ btn = new JButton(letters[f]+i, cup); mainGameWindow.add(btn[i]); btn.addActionListener(this); String stringCommand = Integer.toString(randomArrayNum()); btn.setActionCommand(stringCommand); buttonMap.put(stringCommand,btn); count++; if(count == 18){ generateArray(); } } } 

Then, in the ActionListener , return the button from the command:

 public void actionPerformed(ActionEvent e) { String command = ((JButton) e.getSource()).getActionCommand(); JButton button = buttonCache.get(command); if (null != button) { // do something with the button } } 

Edit

Repeating this answer more than five years later, I have no idea why I suggested HashMap : P

This code does the same without a third-party Map :

 String letters[] = {"0", "a", "b", "c", "d", "e", "f"}; int count = 0; for (int f=1; f < 7;f++){ for (int i=1; i < 7;i++) { String stringCommand = Integer.toString(randomArrayNum()); Button btn = new JButton(letters[f]+i, cup); btn.setActionCommand(stringCommand); btn.addActionListener(this); mainGameWindow.add(btn[i]); // NOTE : I have no idea what this is for... count++; if(count == 18){ generateArray(); } } } 

in ActionListener ...

 public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); String command = button.getActionCommand(); // do something with the button // the command may help identifying the button... } 
+7
source
 JButton btnClear = new JButton("clear"); btnClear.addActionListener(this); btnClear.setName("clear"); //.............. //.............. public void actionPerformed(ActionEvent e) { JButton o = (JButton)e.getSource(); String name = o.getName(); if (name == "clear") { euroMillText.setText(""); } else if (name == "eumill") { getLottoNumbers(); } //JOptionPane.showMessageDialog(null,name); } 
+11
source
 String buttonText = ((JButton) e.getSource()).getText() 
+9
source

Store your buttons in an array and use e.getSource() to find out what it was ...

 Private JButton[] buttons; public void actionPerformed(ActionEvent e) { int index = -1; for (int i = 0; i < buttons.length; i++) { if (e.getSource() == buttons[i]) { index = i; break; } } if (index == -1) { // e didn't come from the buttons } else { // do stuff } } 
+1
source

you have three more options

1) using tools

  • JButton[] buttons;

  • ArrayList<JButton> buttons;

but still you need to determine which JButton pressed from the loop

2) add a separate ActionListener to each JButton

  myButton.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { myButtonActionPerformed(evt); } private void myButtonActionPerformed(ActionEvent evt) { // some Action } }); 

3) add javax.swing.Action in JButton

+1
source

you can set the icon with

 ((JButton)actionEvent.getSource()).setIcon("imageName.png"); //to get the name of button you can use ArrayList<String> buttonNames; buttonNames.add("button"+f+i); 
+1
source

Another option that may work is to use client properties for the button. The JComponent base class provides for setting / getting arbitrary properties:

 Object JComponent.getClientProperty(Object key) void JComponent.putClientProperty(Object key, Object value) 

So, what you can do is set the property value defined by the application for the component, and then poll this property value inside the ActionListener method (just checking that the event source is JComponent).

0
source

All Articles