Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JDialog problems  RSS feed

 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a custom dialog using a subclass of JDialog. I have few problems with this dialog -
1) I want to make sure that when user presses escape, the default "Cancel" operation should take place. Is there a similar API like getRootPane().setDefaultButton(btnDefOK) ?
2)Is there a way to *return* value from the dialog when blocking call dlg.show() returns?
As of now, I am calling a method getResult() to do this, which gives me which button is clicked (OK, Cancel), then I call getUserPreferences() to get the preferences by the user. Is it a proper way of doing this? If not, which is a preferred way?
I have just started experimenting with swing, so pls excuse me if questions are very simple.
TIA,
- Manish
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to 1 is no, there's no equivalent for setting the "default cancel" operation. You'll have to add a key listener to your dialog, and then you can check for the escape key, and call the doClick() of your cancel button when the user has pressed escape.
For 2, your solution is valid. You could create a method that returns the data you want, so your API would look something like
MyDataType myDataTypeObject = someMethodCallThatShowsTheDialogANDgetsTheResultsAfterTheUserClosesIt();
That would simplify your method call, but you still have to perform the work in this method of checking which button was clicked in the dialog, and getting your preferences from it, etc.
Rob
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, you could use the JOptionPane class. This offers a lot of methods that automatically show message dialogs with your own text, title, icon, etc. The methods return a value indicating what button the user presses (you can customize the buttons that appear too). Also, ENTER will trigger an "OK" and ESC will trigger "Cancel".
Check the API documentation for more.
- Daniel
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob
I'll add a listener for Escape key now.
As for JOtionPane, that's not a feasible option(pun intended ) as the GUI is far more complex.
Thanks, both of you,
- Manish
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!