how to get button name on click event in java
Asked Answered
C

5

6

I want to get the name of the button object on clicking the button using Swing. I am implementing the following code:

 class  test extends JFrame implements ActionListener
  {
   JButton b1,b2;
   test()
   {
    Container cp=this.getContentPane();
    b1= new JButton("ok");
    b2= new JButton("hi");
    cp.add(b1);cp.add(b2);
    b1.addActionListener(this);
    b2.addActionListener(this);
   }
public void actionPerformed(ActionEvent ae)
 {
 String s=ae.getActionCommand();
 System.out.println("s is"+s)       ;
} 
}

In the variable s I am getting the command value of the button but I want to get the name of the button, like b1 or b2. How can I get this?

Courteous answered 12/12, 2012 at 3:56 Comment(4)
do u want to get the references or the labels of the buttons ?Subdebutante
i want to get the names of the button not the label of buttonsCourteous
What do you mean the "names"? In your example, what are the names?Glutinous
1) Add a separate listener (or Action) to each button. Problem vanishes. Note that b1 is not the 'name' of any button in that code. Neither of those buttons has a user defined name. 2) Only one of those buttons would be visible. 3) Please use a consistent and logical indent for code blocks.Sorcha
G
10

Use the ae.getSource() method to get the button object itself. Something like:

JButton myButton = (JButton)ae.getSource();
Glutinous answered 12/12, 2012 at 4:1 Comment(0)
R
5

You're asking about getting the variable name, something that you should not be wanting to get as it is misleading and is not really that important and almost doesn't exist in compiled code. Instead you should be concentrate on getting object references, not variable names. If you must associate an object with a String, a clean way to do this is by using a Map such as a HashMap<String, MyType> or HashMap<MyType, String> depending on which you desire to use as the key, but again don't put too much reliance on variable names since non-final variables can change references at the drop of a hat, and objects can be referred to by more than one variable.

For example in the following code:

JButton b1 = new JButton("My Button");
JButton b2 = b1;

which variable name is the name? Both b1 and b2 refer to the same exact JButton object.

And here:

JButton b1 = new JButton("My Button");
b1 = new JButton("My Button 2");

What is the variable name for the first JButton object? Does it matter that the b1 variable doesn't refer to that original object?

Again don't put your faith in variable names as they will often mislead you.

Russelrussell answered 12/12, 2012 at 4:7 Comment(1)
@user1634700: your deleted answer was not bad and in fact was good advice for anyone learning Swing coding. Why delete it?Russelrussell
L
1

If you need the name, there is the function to get it:

getName

but you have to use the setName too.

Lurette answered 12/12, 2012 at 4:5 Comment(0)
S
1

If you want to get the buttons b1, b2 you can have ae.getSource().

If you want the label names of the buttons you can use, ae.getName()

Subdebutante answered 12/12, 2012 at 4:10 Comment(0)
A
0
class  test extends JFrame implements ActionListener
{
   JButton b1,b2;
   test()
   {
    Container cp=this.getContentPane();
    b1= new JButton("ok");
    b2= new JButton("hi");
    cp.add(b1);cp.add(b2);
    b1.addActionListener(this);
    b2.addActionListener(this);
   }
public void actionPerformed(ActionEvent ae)
 {
JButton myButton = (JButton)ae.getSource();
 String s=myButton.getText();
 System.out.println("s is"+s);
 } 
}
Antimacassar answered 19/2, 2021 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.