In the listener for a button in the actionPerformed(ActionEvent e) part, if you call e.getSource it returns a type object. So how can I turn that object that it returns into the original button?

Recommended Answers

cast it
(JButton) e.getSource()

Jump to Post

If you want to know whether it's button1, button2 etc, you can simply say
if (e,getSource() == button1) ...

Jump to Post

All 5 Replies

cast it
(JButton) e.getSource()

If you want to know whether it's button1, button2 etc, you can simply say
if (e,getSource() == button1) ...

So long as you use a period and not a comma. ;-)

Ooops! (hangs head in shame)

You can cast the button eg. (JButton) e.getSource() or make the clicked button do something eg.

            if(e.getSource == button){
            //do something
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.