Java CountDownLatch used to wait for JFrame to dispose
- by Brian
I have referenced this previous question as well as other sources, but cannot get CountDownLatch to work correctly.
Background: mainFrame creates new Frame called dataEntryFrame. When dataEntryFrame "Submit" button is clicked, record added to database and dataEntryFrame disposed. At this point, mainFrame should clear and reload a jList that shows all records.
Issue: When dataEntryFrame loads, java freezes, dataEntryFrame components never load.
I cannot get past this part...
then, in the DataEntryFrame, CountDownLatch should only decrements after the submit button is clicked, successfully adds a record to a database table, and disposes itself. Or when the user clicks cancel...
Code: From MainFrame
CountDownLatch dataEntryDone = new CountDownLatch(1);
DataEntryFrame f = new DataEntryFrame(dataEntryDone);
Thread newThread = new Thread(f);
newThread.start();
dataEntryDone.await();
Code: From DataEntryFrame
public void run(){
initComponents();
loadOtherData();
this.setVisible(true);
}
void submit(){
addRecord();
this.dispose()
dataEntryDone.countDown();
}