Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java listener on dialog close

Tags:

I have a Java app that displays a list from a database. Inside the class is the following code to open a new dialog for data entry:

@Action public void addNewEntry() {     JFrame mainFrame = ADLog2App.getApplication().getMainFrame();     addNewDialog = new AddNewView(mainFrame, true);     addNewDialog.setLocationRelativeTo(mainFrame);     addNewDialog.addContainerListener(null);     ADLog2App.getApplication().show(addNewDialog); } 

How do you add a listener to the main class to detect when the addNewDialog window is closed, so that I can call a refresh method and refresh the list from the database.

like image 920
Woodsy Avatar asked Oct 04 '11 19:10

Woodsy


People also ask

How do I close JDialog swing?

Instead of subclassing the AWT dialog window, the EscapeDialog could subclass the JFC/Swing JDialog window. For all practical purposes, recursively adding key listeners would work, causing the Escape key to close the JFC/Swing dialog window.

What is JDialog in Java?

JDialog is a part Java swing package. The main purpose of the dialog is to add components to it. JDialog can be customized according to user need . Constructor of the class are: JDialog() : creates an empty dialog without any title or any specified owner.


2 Answers

If AddNewView is a Window such as a Dialog or JDialog, you could use the Window.addWindowListener(...). That is, in your main class, you do

addNewDialog.addWindowListener(someWindowListener); 

where someWindowListener is some WindowListener (for instance a WindowAdapter) which overrides / implemetnns windowClosed.

A more complete example, using an anonymous class, could look like

addNewDialog.addWindowListener(new WindowAdapter() {     @Override     public void windowClosed(WindowEvent e) {         refreshMainView();     } }); 

Relevant links:

  • Official Tutorial: How to Write Window Listeners
like image 154
aioobe Avatar answered Oct 07 '22 19:10

aioobe


you have to add WindowListener and override windowClosing Event, if event occured then just returs some flag, example here

like image 20
mKorbel Avatar answered Oct 07 '22 20:10

mKorbel