Listening to an ActionListener from another class?
I have a JPanel with a JButton.
publicclass OkButtonPanelextends javax.swing.JPanel{
public OKButtonPanel(){
JButton okButton =new JButton("OK");
okButton.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent ae){
System.out.println("You pressed the ok button!");
}
});
......
I add this JPanel to a JDialog. I want this dialog to close whenever the ok button is pressed.
I know I can do something easy yet not exactly correct like make the okButton public and then add an ActionListener:
...
jdialog.setVisible(true);
okButtonPanel.okButton.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent ae){
System.out.println("You pressed the ok button, closing dialog");
jdialog.setVisible(false);
}
});
...
But how do I make it more robust? How do I "listen" to events that happen inother objects and take the appropriate action onmy object?

