Swinging Task Dialog (part 2)


This week’s focus was on adding more functionality.

The API was extended with an ability to set commands (buttons on the bottom of the dialog). TaskDialog features a set of standard commands ( currently only Ok and Cancel ). But it is very easy to add any arbitrary command. The only requirement is that the command has to implement TaskDialog.Command interface.

TaskDialog’s getResult() method returns last executed command. A simpler way to use it is show() method which shows the dialog and returns last executed command. Here is a warning dialog:

TaskDialog dlg = new TaskDialog("Security Warning");
dlg.setInstruction( "The publisher cannot be verified.\nDo you want to run this software?" );
dlg.setText( "Name: C:\\Program Files\\eclipse\\eclipse.exe\n" +
      		     "Publisher: <b>Unknown Publisher</b>\n" +
       		     "Type: Application\n");
dlg.setIcon( TaskDialog.StandardIcon.WARNING );
dlg.getFooter().setCheckBoxText("Always ask before opening this file");
dlg.setCommands( StandardCommand.OK.derive("Run"), StandardCommand.CANCEL );
if ( dlg.show().equals(StandardCommand.OK)) {
       	//do something
}

Sometimes custom command has to not just close the dialog but execute some additional logic.  To accomplish this PropertyChangeListener can be attached to the TaskDialog’s “result” property. As soon as result changes (command button is pressed ) your code will be notified.

It is possible now to add any fixed component to the dialog ( in addition to expandable one ). Here is the example of the  dialog with the progress bar.

TaskDialog dlg = new TaskDialog("Copying...");
dlg.setInstruction("Copying file");
dlg.setText( "Location: From 'Others' to 'Others'\n" +
      		     "File Name: <b>Photo.jpg</b>" );
dlg.setIcon( TaskDialog.StandardIcon.INFO );

JProgressBar pb = new JProgressBar();
pb.setValue(30);
dlg.setFixedComponent( pb );
dlg.show();

More work is required in the area as I want to to provide prebuilt components such as command links.

To even further simplify usage I added TaskDialogs class. It makes creation of certain task dialogs a one liner.

TaskDialogs.inform( "You've won!", "The game is over with the 15:3 score");
if (TaskDialogs.warn( "Are you sure you want to quit?", "Please do not quit yet!")) {
        try {
	        new BigDecimal("seven");
       } catch( Throwable ex ) {
	       TaskDialogs.showException(ex);
       }
}

The above code produces three task dialogs:

The project is available at http://code.google.com/p/oxbow/ under BSD license.
As always – comments and suggestions are welcome.

Advertisements

4 comments

  1. Hello Eugene,
    i just want to say thank you for this wonderful piece of software. This is a major step forward towards a really usable standard dialog API in swing. We are looking forward to include your work in some of our projects.

    Awesome!

  2. Thank you for the kind words! If you start using it – please let me know where and how 🙂
    And of course do not hesitate to inform me about issues or suggestions

  3. Pingback: Java desktop links of the week, March 15 | Jonathan Giles

  4. Pingback: Prettier failures using Swing TaskDialog « Schneide Blog


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s