user1162715
user1162715

Reputation:

How do I get the text of a button in Java?

class Test extends JPanel implements MouseListener
{
    private JButton b1, b2, b3, b4;

    public Test()
    {
        setLayout (new GridLayout (2, 3));

        b1 = new JButton ("Button 1");
        b2 = new JButton ("Button 2");

        add (b1);
        add (b2);
    }

    public void mousePressed (MouseEvent event)
    {
        if (event.getText() == b1.getText())
        {
        }
    }
}

I was wondering if there was a method within the MouseListener or JPanel class that allows me to get the text of a button that is clicked. Thanks

Upvotes: 6

Views: 45461

Answers (4)

Roy Slezak
Roy Slezak

Reputation: 21

This was driving me nuts too... and here's the answer: there is no .getText() method for a Button... you want to use .getLabel() Aren't namespaces fun?

Upvotes: 1

user2598067
user2598067

Reputation: 1

import java.util.Arrays;

import java.util.List;

private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {

//********************************************************

        String Hunt1 = evt.toString();
        Hunt1 = Hunt1.replaceAll("=", ",");
        Hunt1 = Hunt1.replaceAll("]", ",");
        String [] Hunt2 = Hunt1.split(",");
        List Hunt3 = Arrays.asList(Hunt2);
        String ButtonText = Hunt3.get(2).toString();
        System.out.println(ButtonText); }

//*******************************************************

This is how I did it. I was looking for a way to pass button names out to a method so that I could run down an if(s==Button1){}else if(s==Button2){..... scope of about 12 buttons.

Catching a button name proved difficult for me (I have only been doing Java for a day or so) so I came up with this to pass the label/text out to a method.

That code can be placed in its own method so that after you make your button set you can just go into each click event and paste

SendButtonLabel(evt.ToString()); 

then pass it from there to your if/then buttonlist method

or whatever your project needs without having to actually go in and hardcode anything into each click event. I suppose that it would be possible if you were doing a particular project to set up your IDE to auto-add that method call to each click event as it is created, but I am not that far advanced as of yet.

Upvotes: 0

Benjamin
Benjamin

Reputation: 3448

You should try looking at ActionListener and implement it. Those Actions are fired, whenever you click a button, this is how you should work:

class Test extends JPanel implements ActionListener {
...
   public Test() {
        super();
        ...
        b1 = new JButton("Button 1");
        ...
        add(b1);
        ...
        b1.addActionListener(this);
        ...
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        System.out.println(((JButton) event.getSource()).getText());
        if (event.getSource() == b1) {
            // do sth
        } else if (event.getSource() == b2) {
            // do sth else
        }
    }
}

Then write a main method and add a new instance of Test panel to a JFrame and see how it works.

Tutorial can be found on the following website: http://docs.oracle.com/javase/tutorial/uiswing/components/button.html

Upvotes: 5

Sean
Sean

Reputation: 7737

Take a look at The MouseEvent api There is a method getSource() which you can use that returns the object where the event occurred. Then check if that object is an instance of a button. If it is you can cast it to a button type and then get the text from there.

public void mousePressed (MouseEvent event){
   Object o = event.getSource();
   JButton b = null;
   String buttonText = "";

   if(o instanceof JButton)
     b = (JButton)o;

   if(b != null)
     buttonText = b.getText();

}

Upvotes: 5

Related Questions