开发者

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 t开发者_运维技巧he 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;
}


  • 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() ...


This has nothing to do with use of invokeAndWait or invokeLater and all to do with listening on one thread for a response in another. If you're not using a modal dialog for this, such as a JOptionPane (which is quite easy to use and can hold a very comnplex GUI if needed), consider using a listener or otherwise known as the observer design pattern to notify the non-GUI component when the GUI has been acted upon.

Also, is GlossaryEntryMergeUI the only GUI the user comes in contact with? Or is it called into being from another GUI, perhaps a "parent" GUI? If the latter, then a modal dialog, not a JFrame is the way to go. Edit: or a CardLayout as per mKorbel's excellent answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜