• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JDialog problems

 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic