Search code examples
javaswingswingx

Using invokeLater or invokeAndWait to wait for a response from another GUI


This method is supposed to merge two the definitions of two entries for a single in a glossary. It creates an instance of the GlossaryEntryMergeUI class (extension of JFrame), which walks the user through the merging process. The user clicks a button to submit and the window closes. The merge method extracts the merged definitions and returns the combined glossary entry.

What is the best way to make the merge method wait for response from the MergeUI? I tried using InvokeAndWait, but I couldn't figure out how to make it work.

public GlossaryEntry merge(GlossaryEntry otherEntry){
    //First, merge the definitions
    GlossaryEntryMergeUI thisMerge = new GlossaryEntryMergeUI(this,otherEntry,mergeSignal);
    thisMerge.setVisible(true);
    thisMerge.setAlwaysOnTop(true);

    GlossaryEntry combined = new GlossaryEntry(word);
    combined.addDefinitions(thisMerge.getDefinitions());

    return combined;
}

Solution

    • consider to look for CardLayout instead of switch between two or more Windows

    • if is there Serializable or Observate, then better is wrap the code into invokeAndWait(), for simple switch between two Windows is there invokeLater()

    • I'd suggest not using two JFrames in same time, one JFrame and another Window would be JDialod or JWindow, because (for example if is there BackGroung Task) sometimes is so hard swith Focus from one JFrame to another, not to move JFrame toFront() ...