You are not logged in.

#1 2005-03-22 21:48:20

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

JDialog Ok/Cancel button events

Dear Java gurus,

I have something that's been bugging me for days. Now, maybe it's really obvious but my cold has bunged up my head too much to think clearly. Anyway, I thought I'd pass it this way for expert opinion..

I have a class that extends JDialog, with various textboxes, comboboxes etc. It also has the usual OK + Cancel buttons. The only thing left to implement is something like this:

MyCustomDialog d = new MyCustomDialog();
int returnVal = d.showDialog();

This is similar to most of the dialogs provided with Swing, where the return value is a static field something along the lines of 0 == OK button pressed, 1 == Cancel.

This allows me to only start "getting" the values from the dialog only if the user selects ok. The problem is, I can't see how to accomplish this. I've been looking at the source for JFileChooser and JColorChooser, but they are more complicated than I can follow. (I am relatively novice when it comes to Swing) I promise, I have been looking on the net for examples, but I've had no luck. Maybe because any searching for JDialog tends to bring back simple examples of the supplied Swing dialogs.

So, any suggestions?

TIA

Offline

#2 2005-03-22 22:05:49

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: JDialog Ok/Cancel button events

IIRC, you want to use JOptionPane.  Create a Panel with all the options on it (not a JDialog). Then call one of the JOptionPane showXXXDialog() methods, passing the panel as the object parameter and setting it up to use OK and cancel as the buttons (there are several variations of these methods in the JOptionPane class with various constructors).

You may be able to use JOptionPane.showConfirmDialog, or you might have to custom build it with JOptionPane.showOptionDialog. Look at the javax.swing.JOptionPane API for more info (http://java.sun.com/j2se/1.5.0/docs/api … nPane.html)

Dusty

Offline

#3 2005-03-22 22:25:40

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: JDialog Ok/Cancel button events

i'm just guessing, but after the "showDialog" method completes and the dialog is no longer shown, aren't the internal controls still valid objects? can't you still grab the text from a textbox at that point?

Offline

#4 2005-03-22 22:32:10

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: JDialog Ok/Cancel button events

yeah, that's how you get the data.The problem is that if you just have a method showDialog, it returns immediately, even before the user presses OK. The way to get around this is have the showDialog() wait for ok or cancel to be pressed, which entails making another thread, an event listener, etc. I believe this is what JOptionPane does, but in the case of JOptionPane, its been done for you.

Most people don't realize that you can pass a component as an option to JOptionPane. They think that JOptionPane.showMessageDialog(parent, "this is a message") is only useful for showing something like a JavaScript Alert. However, the signature is showMessageDialog(Component parent, Object message), and the message can be a component. Thus, you can add an entire panel and have the JOptionPane take care of waiting for OK to be clicked.

The question arooaroo was asking, I think, wasn't how to access the data in a TextField (for example), but how to know whether ok or cancel was pressed and how to wait for that to happen.

Dusty

Offline

#5 2005-03-22 23:43:27

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: JDialog Ok/Cancel button events

Dusty wrote:

The question arooaroo was asking, I think, wasn't how to access the data in a TextField (for example), but how to know whether ok or cancel was pressed and how to wait for that to happen.

ah, I get it... it didn't make sense... maybe because I didn't know that the showDialog method returned immediately.

Heh, I'd use a mutex just to be dorky... the showDialog function locks the mutex and doesn't unlock it until ok/cancel was pressed... the main thread tries to lock the mutex directly after the showDialog call, and hangs until the mutex is released... it'd be funny...

that is of course, assuming the event handling works so as to not hang the whole event loop as well...dunno what java does...

Offline

#6 2005-03-23 08:52:00

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: JDialog Ok/Cancel button events

Dusty wrote:

IIRC, you want to use JOptionPane.

This did occur to me shortly after posting (but I would say that, wouldn't I?!) I hope to investigate later, but I've got this cold/flu hybrid illness that is making feel very rough. (it's taken me 5mins just to write this!)

Anyway, cheers guys - you've been as helpful as always.

Offline

#7 2005-03-23 11:54:14

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: JDialog Ok/Cancel button events

Dusty wrote:

IIRC, you want to use JOptionPane.  Create a Panel with all the options on it (not a JDialog). Then call one of the JOptionPane showXXXDialog() methods, passing the panel as the object parameter and setting it up to use OK and cancel as the buttons (there are several variations of these methods in the JOptionPane class with various constructors).

Ok, finally got around to having a look at what JOptionPane can do. When you talk about "passing the panel", it turns out that the Component represents the "parent component" so that it can display the dialog relative to  that component. It doesn't mean that the Component passed in will be displayed in the JOptionPane sad  Shame, because I thought you were on to something.

I'll have to keep looking..., or maybe I'll try the mutex approach. Seems plausible - although not exactly The Java Way!
(Cheers btw)

Offline

#8 2005-03-23 14:34:56

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: JDialog Ok/Cancel button events

The solution....

public int showDialog() {
        show();
        return returnVal;
}

The trick was to ensure that the JDialog was model (which I wasn't doing). This means that show() doesn't finish _until_ the dialog is closed. The returnVal instance variable is by default set to 2. Then, in the actionListeners for the ok and cancel buttons, I set returnVal to 0 and 1, respectively, and then dispose() the dialog. This ensures that returnVal isn't returned until a button is pressed.

Job's a goodun. (Shame it's taken me a week to work out!)

Offline

#9 2005-03-23 17:09:42

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: JDialog Ok/Cancel button events

arooaroo wrote:

Shame, because I thought you were on to something.

I was. Trust me.

Look at the signature for JOptionPane.showConfirmDialog(). This is the simplest version, but the others look like this and then some:

static int showConfirmDialog(Component parentComponent, Object message)

Note that the message parameter is of type  object. This means you can theoretically pass any type of object. A component is an object. Therefore, you can pass a JPanel as the message parameter.

Look at this quote from the JOptionPane description in the API:

message
    A descriptive message to be placed in the dialog box. In the most common usage, message is just a String or String constant. However, the type of this parameter is actually Object. Its interpretation depends on its type:

    Object[]
        An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack. The interpretation is recursive -- each object in the array is interpreted according to its type.
    Component
        The Component is displayed in the dialog.
    Icon
        The Icon is wrapped in a JLabel and displayed in the dialog.
    others
        The object is converted to a String by calling its toString method. The result is wrapped in a JLabel and displayed.

I was going to suggest the modal option on JDialog, but for some reason I thought it wouldn't work.  I still think using JOptionPane would be easier; that's what it was designed for. wink

Dusty

Offline

#10 2005-03-23 17:33:48

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: JDialog Ok/Cancel button events

Dusty wrote:

Note that the message parameter is of type  object. This means you can theoretically pass any type of object. A component is an object. Therefore, you can pass a JPanel as the message parameter.

Ooooooh. I getcha now. I didn't look that closely at the message argument (obviously!) Hmmm... well, I do have my JDialog working fine now, but I may still have a go later with the JOptionPane again - now that I know what to do!

Cheers

Offline

#11 2005-03-23 17:39:53

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: JDialog Ok/Cancel button events

arooaroo wrote:

Ooooooh. I getcha now. I didn't look that closely at the message argument

My fault, I didn't explain it too well... We're both sick ATM, eh?

Dusty

Offline

#12 2005-03-23 19:04:42

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: JDialog Ok/Cancel button events

Dusty wrote:

My fault, I didn't explain it too well... We're both sick ATM, eh?

The only person I knew with a cold before mine came on was you - are you spreading viruses over the Internet Dusty?! tongue

Offline

Board footer

Powered by FluxBB