• Post Reply Bookmark Topic Watch Topic
  • New Topic

Input without action listeners: JOptionPane?

 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can someone recommend a way to display several input fields in a
dialog, which brings strings into the method, without using
action listeners?

I am writing Java code to create new drawings in a CAD application.
I can bring in strings one at a time using JOptionPane.showInputDialog
and the program runs just fine. I can also create a nice JPanel with
multiple labels and text fields but the action listener seems to crash
my CAD application.

The strings, i.e. drawing name, approved by, date, need to be fed into a method which then creates the drawing using that information.

I was wondering if there was a facility similar to showInputDialog which would display multiple input fields or a way to modify that method to allow this. TIA.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what you mean exactly by "without ActionListeners," but basically once you move beyond what JOptionPane offers, you need to start using your own custom subclasses of JDialog. You can make arbitrary input dialogs that way.

Ahhh, I remember you. OK, now I know what you're saying about "no action listeners:" you just want to invoke the dialog and get the answers back, the way JOptionPane does. You're going to have to implement what JOptionPane does internally: use the wait() and notify() methods to communicate between your main thread and the GUI thread, so that your main thread waits until "OK" is pressed in the GUI. We can certainly help you learn to do this, if you're interested.
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I am very interested in learning to use the wait and notify methods you mention. I looked at those a little bit but wasn't really sure if they were what I needed. How would I go about learning to implement what JOptionPane does internally? Thanks again.
 
Jerry Goldsmith
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just discovered the src.zip file in my JDK folder. Sweet. I am reviewing the source code for JOptionPane presently.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand the wait/notify idea. Wouldn't you just need a normal modal dialog?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, what's that notion of ActionListeners crashing your application? How would they do that?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
I'm not sure I understand the wait/notify idea. Wouldn't you just need a normal modal dialog?


He's writing a program which drives some external code from a main (non-GUI) thread, and so far has just been grabbing parameters using JOptionPane. He's been stalwartly resisting building something event-driven. I think at this point we're over the hump and it's easier to just go with the flow. He needs to be able to call a method like "getSomeParameters()" and wait for it to return.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't something along the lines of



work?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would work if "show()" blocked until the dialog was disposed or something -- but it doesn't. Modal-ness just means that input to other windows is blocked; it doesn't affect the thread that creates the dialog.

I'm basically saying he needs to write a method that does what you're thinking "show()" does (show() is actually deprecated; it's just the same as "setVisible(true)".) Unless I'm missing something obvious, there's nothing that does quite this built into the API.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Unless I'm missing something obvious, there's nothing that does quite this built into the API.


Depending on exactly what you want it can be as easy as:
JOptionPane has a decent amount of flexibility.
[ February 08, 2007: Message edited by: Brian Cole ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!