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
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.