Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting input from JDialog  RSS feed

 
Sri Rangan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am opening a JDialog from a JPanel to gather user input . When user closes the JDialog, I need to return the values back to the JPanel.
I understand that I could add a windowListner to the JDialog to notify the JPanel while closing. How can I pass the data from JDialog to the JPanel
Thanks for your reply
Sri
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really depends on how much data you are wanting to extract from the JDialog. If it is something as simple as 1 piece of data, then you don't need to use a JDialog. Use a JOptionPane instead.
However, is the data become more complex, there are several ways to tackle this.
1. Pass a Data Structure to your JDialog constructor to be populated when an OK button is clicked on the JDialog:
DataObject data = new DataObject();
MyDialog dialog = new MyDialog(data);
You would do this inside your JPanel or wherever, so your JPanel has a reference to this Data Object when you close your JDialog.
2. Create all the Data Variables in your JPanel, making them public, and then pass a reference to your JPanel to the JDialog in the constructor:
MyDialog dialog = new MyDialog(mypanel);
mypanel.data1 = dialogdata1;
mypanel.data2 = dialogdata2;
...etc
3. Create public static variables in your JPanel and then just reference them from your JDialog if they are in the same package:
mypanel.staticData1 = dialogdata1;
mypanel.staticData2 = dialogdata2;
...etc.
These are just a few ideas to get your started.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!