Get return value from new class with gui
I have a class that extends JFrame to make orders. in the middle I have a button that opens a new window that is used to find an article.
What I need is: When I click btnNewArticle, after searching new article, and confirm in new window, I will get as return article code.
Click btnNewArticle --> (open new window to find an article) --> 开发者_Python百科confirm selection -->as return I get Article Code.
Is it possible?
Thanks
Have a look at Java dialogs: How to Make Dialogs.
In the above tutorial, they explain how to do custom dialogs such as the one below:
(source: oracle.com)
They handle return-values by giving the child-dialog a reference to it's parent dialog, in which it fills in the relevant values upon closing.
For me this principle has worked:
public class ArticleSearchDialog extends JDialog {
public static ArticleId execute(Frame parent) {
ArticleSearchDialog dialog = new ArticleSearchDialog(parent, true);
dialog.setVisible(true);
return dialog.getSelectedArticle();
}
private ArticleId getSelectedArticle() {
return selectedArticle;
}
private void jbCancelActionPerformed(ActionEvent evt) {
selectedArticle = null;
setVisible(false);
dispose();
}
private void jbOkActionPerformed(ActionEvent evt) {
selectedArticle = ...; //TODO
setVisible(false);
dispose();
}
// All the rest of the dialog code.
}
Then the call gets really easy:
ArticleId articleId = ArticleSearchDialog.execute(this);
if (articleId != null) {
//TODO
}
精彩评论