Stupid dialog closing question

K

Koos Pol

Hi cljp,

I've hammered my brains on it but I can't seem so figure out why this dialog
doesn't close after pressing "Save", or "OK".

What do I need to do to auto close this dialog after pressing a button?

JButton fooButton = new JButton("Foo!");
JButton barButton = new JButton("Bar!");
JOptionPane pane = new JOptionPane();
pane.setInitialValue(fooButton);
pane.setMessage("Foo & Bar");
pane.setMessageType(JOptionPane.PLAIN_MESSAGE);
pane.setOptions(new JButton[] {fooButton, barButton} );
JDialog dialog = pane.createDialog(null, "Foobar");
dialog.setVisible(true);

BTW, I've not chosen the showXXXdialog version for flexibility reasons not
shown here.

Thanks,
Koos
 
K

Koos Pol

Ok. After a day of fiddling and reading tutorials, I came up with the
following solution. I wrapped the lot into it's own class with it's own
ActionListener:


public class FooBar extends JPanel implements ActionListener {

private JDialog dialog;

public FooBar (){
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JButton fooButton = new JButton("Foo!");
fooButton.addActionListener(this);
JButton barButton = new JButton("Bar!");
JOptionPane pane = new JOptionPane();
pane.setInitialValue(fooButton);
pane.setMessage("Foo & Bar");
pane.setMessageType(JOptionPane.PLAIN_MESSAGE);
pane.setOptions(new JButton[] { fooButton, barButton} );
dialog = pane.createDialog(null, "FooBar");
dialog.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
System.err.println("Button Foo!");
dialog.setVisible(false);
}


Is this the way to make windows auto close after pressing a button?


Koos
 
A

Andrew Thompson

Koos Pol wrote:
...
Is this the way to make windows auto close after pressing a button?

It is one way. It depends on what you want the 'window'
to do after the button is clicked. Here is another example
that might suit the immediate purpose better.

<sscce>
import javax.swing.JOptionPane;

public class FooBar2 {

public FooBar2 () {

String[] options = {"Foo", "Bar", "Too"};
int choice = JOptionPane.showOptionDialog(
null,
options[0] + " & " +
options[1] + " & " +
options[2],
"" + options[0] + options[1] + options[2],
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options,
options[1]
);
if (choice<0) {
System.out.println( "No choice made" );
} else {
System.out.println( "choice: " + options[choice] );
}
}

public static void main(String[] args) {
FooBar2 fb2 = new FooBar2();
}
}
</sscce>

Note this example shows how to return data to
the calling process. There are other ways to close
frames and dialogs that might also make sense,
such as adding a WindowListener or calling the
setDefaultCloseOperation method for a frame.
 
K

Koos Pol

Thanks Andrew, that's clear.

But the point of the exercise was to accomplish this without
JOptionPane.showXXXDialog (for flexibility reasons). Closing a window using
JOptionPane.showXXXDialog is (almost?) implicit.
So creating a dialog using the JOptionPane.createDialog pragma and then
closing it. Perhaps you can comment on that with my brew at hand?

Thanks,
Koos
 
A

Andrew Thompson

Koos said:
Thanks Andrew, that's clear.

But the point of the exercise was to accomplish this without
JOptionPane.showXXXDialog (for flexibility reasons).

Oh wait.. you mean like you said at the bottom of
the first post? "I've not chosen the showXXXdialog
version for flexibility reasons not shown here."

I missed that. Oops!
...Closing a window using
JOptionPane.showXXXDialog is (almost?) implicit.
So creating a dialog using the JOptionPane.createDialog pragma and then
closing it. Perhaps you can comment on that with my brew at hand?

OK well.. I really still don't understand quite
what you are trying to achieve here. The 'foos'
and 'bars' do not help - can you turn that into
'browse' 'open' 'cancel', or somthing meaningful?
Is the object to return a single selected option?
Else, ..what?

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200705/1
 
K

Koos Pol

OK well.. I really still don't understand quite
what you are trying to achieve here. The 'foos'
and 'bars' do not help - can you turn that into
'browse' 'open' 'cancel', or somthing meaningful?

Definitely. But that doesn't teach me anything. I'm trying to understand the
mechanism of the sucker. What would I have to do if
JOptionPane.showXXXdialog doesn't fit the bill? I find the whole shebang of
GUI's, listeners, callbacks and what not a big pool to drown in. It's a
matter of education, raelly :)
Is the object to return a single selected option?

Yes. Pobably. An example could be a dialog with a grid of of buttons (a
puzzle perhaps) where one click of a button closes the window and leads you
to the next.

Koos
 
A

Andrew Thompson

Koos said:
Definitely. But that doesn't teach me anything. I'm trying to understand the
mechanism of the sucker. What would I have to do if
JOptionPane.showXXXdialog doesn't fit the bill?

You might be right there. JOptionPane can be handy
for those situations where you really don't need to
customize it much. Anything much beyond the trivial,
and it is easier to code the entire thing as a JDialog,
rather than trying to 'make JOP fit'.
Yes. Pobably. An example could be a dialog with a grid of of buttons (a
puzzle perhaps) where one click of a button closes the window and leads you
to the next.

JOP is well suited to returning 'one of various' options, as
shown in my example. Once more sophisticated layout
(style, subtlety etc.) comes into it - I would look to a JDialog.
 
K

Koos Pol

Thanks a bunch for your remarks, Andrew. You've been very helpful.

Cheers,
Koos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top