Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Stupid dialog closing question

Thread view: 
Koos Pol - 05 May 2007 16:44 GMT
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
Signature

43rd Law of Computing: Anything that can go wr
fortune: Segmentation violation -- Core dumped

Koos Pol - 05 May 2007 16:47 GMT
> doesn't close after pressing "Save", or "OK".

Duh... "Foo!" or "Bar!"

Koos
Signature

43rd Law of Computing: Anything that can go wr
fortune: Segmentation violation -- Core dumped

Koos Pol - 06 May 2007 14:19 GMT
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
Signature

43rd Law of Computing: Anything that can go wr
fortune: Segmentation violation -- Core dumped

Andrew Thompson - 06 May 2007 14:55 GMT
...
>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.

Signature

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

Koos Pol - 06 May 2007 18:03 GMT
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
Signature

43rd Law of Computing: Anything that can go wr
fortune: Segmentation violation -- Core dumped

Andrew Thompson - 06 May 2007 18:36 GMT
>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?

Signature

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

Koos Pol - 07 May 2007 17:51 GMT
> 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
Signature

43rd Law of Computing: Anything that can go wr
fortune: Segmentation violation -- Core dumped

Andrew Thompson - 07 May 2007 23:55 GMT
>> OK well.. I really still don't understand quite
>> what you are trying to achieve here.  The 'foos'
[quoted text clipped - 4 lines]
>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'.

>> 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.

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.

Signature

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

Koos Pol - 08 May 2007 16:03 GMT
Thanks a bunch for your remarks, Andrew. You've been very helpful.

Cheers,
Koos
Signature

43rd Law of Computing: Anything that can go wr
fortune: Segmentation violation -- Core dumped



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.