Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing Component Event Handlers

I have a menu class called Menu wich defines a JFrame, this class instantiates two JPanel objects which contain text fields and buttons. There is a private inner class inside each JPanel extending class which handles the events triggered by pressing the buttons.

I want to know if there is a way for the Menu class to detect the triggered events from the buttons inside the JPanels.

Could you add a listener to a JPanel in the Menu class and do something like this? :

if(event.getSource() == panel.getButton1()){ 
    //do button1 code 
} 
like image 581
lilroo Avatar asked Nov 20 '25 10:11

lilroo


1 Answers

JPanels and in fact all Swing components have property change support, and so you could easily add a PropertyChangeListener to any Component and listen for state changes. Just be sure that in the code whose state is changing to call firePropertyChange(...) after the state changes.

like image 178
Hovercraft Full Of Eels Avatar answered Nov 21 '25 23:11

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!