my dog learned polymorphism
The moose likes Swing / AWT / SWT and the fly likes Input without action listeners: JOptionPane? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Input without action listeners: JOptionPane?" Watch "Input without action listeners: JOptionPane?" New topic
Author

Input without action listeners: JOptionPane?

Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
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
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
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

Joined: Nov 29, 2006
Posts: 53
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

Joined: Jul 11, 2001
Posts: 14112
I'm not sure I understand the wait/notify idea. Wouldn't you just need a normal modal dialog?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Also, what's that notion of ActionListeners crashing your application? How would they do that?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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

Joined: Jul 11, 2001
Posts: 14112
Wouldn't something along the lines of



work?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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

Joined: Sep 20, 2005
Posts: 852
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 ]

bitguru blog
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Input without action listeners: JOptionPane?
 
Similar Threads
Bringing my action listener in line
Disappearing showInputDialog
Java Script Validation on Applet fields
Comments on my first program to use an object
Cancel a call to the repaint() method