2
  @Override
  public void actionPerformed(ActionEvent e) {
      if (e.getSource() == thirdBtn) {
          //System.out.println("Third Button Click");
          System.out.println(e.getSource()+" Click");
      }
  }

In the code above, I was wondering if instead of doing this:

//System.out.println("Third Button Click");

if I could do something like this:

System.out.println(e.getSource()+" Click");

However the code outputs:

BlackJack.OverBoard$BlackJackButton[,440,395,100x25,alignmentX=0.0,alignmentY=0.5,
    border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@7a3d8738,
    flags=16777504,maximumSize=,minimumSize=,preferredSize=,
    defaultIcon=,disabledIcon=,disabledSelectedIcon=,
    margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],
    paintBorder=false,paintFocus=true,
    pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,
    text=Change,defaultCapable=true] Click

I don't want this, I want to know how to get the JButton name and output it on click.

EDIT:

Some people are confused. When I say "name" (maybe that's the wrong word for it), I meant say you initialize a JButton

JButton btnExample = new JButton();

I want it so that when you click the button, it outputs btnExample in the console.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Exikle
  • 1,155
  • 2
  • 18
  • 42
  • 2
    Can't be done...the way you have it now. You have three choices. You can either use `JButton#setText` and `JButton#getText` which is the text displayed on the screen, `JButton#setName` and `JButton#getName` which is an internal `String` reference, so it can be anything you like, or, you can create your own custom button and modify the `toString` method - which is not recommended... – MadProgrammer Jan 14 '13 at 01:52
  • 3
    *"I want it so that when you click the button, it outputs `btnExample` in the console."* Since this information is probably not visible to the end user of a GUI and would mean nothing to them if they saw it, it seems this use-case refers to code running during development. I suggest that what is actually needed here is a debugger, and perhaps some logging. – Andrew Thompson Jan 14 '13 at 04:41
  • @AndrewThompson i understand this but it was just me wanting to know for educational purposes. Thank you – Exikle Jan 15 '13 at 00:10

2 Answers2

11

You can cast to a JComponent if you know that only JComponents will be the return value of e.getSource() I'm using JComponent as the cast since it gives more flexibility. If you're only using JButtons, you can safely cast to a JButton instead.

  @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == thirdBtn) {
                    //System.out.println("Third Button Click");
                    System.out.println(((JComponent) e.getSource()).getName()+" Click");
                }
            }

Feel free to replace getName() with getText(), depending on what exactly you need.

Also, the == operator should only be used to compare Object references, so consider casting to a JComponent from the beginning and using .equals() on the names or text.

Edit You can't output the name of the variable, but you can set the name/text of the JComponent. Eg

JButton btnExample = new JButton();
btnExample.setName("btnExample");

Or if you want "btnExample" to actually be displayed on the button:

JButton btnExample = new JButton();
btnExample.setText("btnExample");
A--C
  • 36,351
  • 10
  • 106
  • 92
  • 1
    (While I know the OP has said "name", most developers never set a components "name", instead, you might want to include an example using `getText` as well ... IMHO) – MadProgrammer Jan 14 '13 at 01:43
  • @MadProgrammer You just get auto-notified, sorry! I suspect it had to do with me forgetting the `e`, but who knows? – A--C Feb 07 '13 at 00:52
  • 2
    It would nice if down voting actually required the voter to leave a comment! – MadProgrammer Feb 07 '13 at 01:01
6
System.out.println(((JButton) e.getSource()).getName() + " Click");
syb0rg
  • 8,057
  • 9
  • 41
  • 81