简体   繁体   中英

How to get and print the name of a variable which holds a JButton?

I was wondering if it is possible to get and print a JButton 's name?

Given this code:

JButton button1 = new JButton();

How can I get back button1 ?

I have tried:

button.getName()

but it returns

null

My guess - you really want the text displayed by the JButton, not its name property. If so, check out,

button.getText();
// or
button.getActionCommand();

If you're looking for the variable name, well that is not obtainable by method call, nor should it be since an object can be referred to by many variables, or may be part of a collection or array, and so a variable name for an object is close to a meaningless concept.

Moral: clarify your question by giving us your background story, just what it is you're trying to do, not so much how you're trying to do it.


Edit
You state in comment,

get the name of the button, for example, if I have a button called JButton button1 = new JButton(); I want to print the name of this button which is button1

You're confusing variable names with object names. button1 above is the name of the JButton variable but not the object it refers to since objects don't have names.

It's not generally possible to get an object's variable "name" by calling a method on object, because many objects don't even have names, for instance if they're part of a collection, or a single object may be referred to by many variables, and so which "name" would be the one you desire.

For instance say you have this code:

JButton button1 = new JButton("Foo");
JButton button2 = button1;

What is the "name" of the JButton object created here? Both button1 and button2 refer to the exact same JButton object, and so there's no such thing as a name for an object here.

Again, you're not telling us what you're trying to do, but rather how you're trying to do it. Your question is a classic XY Problem and only will be solved when you tell us the overall problem.

If you're trying to find out which JButton has been pressed, the ActionListener's actionPerformed ActionEvent parameter has a getSource() method which will return a reference to the button object that was pressed, and you can use that. How you use that will depend on the details of your problem which you need to tell us.

For example:

// inside of an ActionListener
public void actionPerformed(ActionEvent evt) {
  // this will get you a reference to the button object that was pressed:
  AbstractButton sourceBtn = (AbstractButton) evt.getSource();

  // now you can use the sourceBtn object, or compare its reference to your 
  // JButton variables or collections in your GUI
  // for example if your GUI has buttons in a List<JButton> you could
  for (JButton button : buttonList) {
    if (sourceBtn == button) {
      // you've found your button of interest!
    }
  }
}

Swing name properties are null by default. You need to set them

button.setName("name");

If you want to get the text on the button then you need to use the method getText(). So the solution for you would be.

button.getText()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM