jannissary
jannissary

Reputation: 91

JDialog vs JOptionPane vs JPanel For Show Message

I'm developing an application with Java Swing and sometimes I need to show messages in case of these situations:

  1. When user click to "add" button, it takes relatively long times because of TCP connection. I'm using a JPanel to show "processing..." to user. When user click to "add" button, I change the setVisible(true) of the panel which contains "processing..." message.

  2. When it is added correctly, I show a message "added" to user in the same way (setVisible)

  3. When the user enter wrong input I show a message in the same way.

To do this, I create different panels and I customized it according to my design. But when I use JDialog or JOptionPane I can't fully customize.

I want to ask that, is it a wrong approach? Does it cause performance and visualization problems? Should I use JOptionPane or JDialog to achieve these processes?

Upvotes: 1

Views: 3110

Answers (1)

Jason Braucht
Jason Braucht

Reputation: 2368

JPanel is simply a container used to house other components.

JDialog is a general purpose dialog which can be customized by adding other components. (See How to add components to JDialog for more on this)

JOptionPane can be thought of as a special purpose dialog. From the javadoc (emphasis added):

JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something.

If you dig into the source for JOptionPane you'll find that the methods like showInputDialog() actually create a JDialog, then populate it with the JOptionPane

public static Object showInputDialog(Component parentComponent,
    Object message, String title, int messageType, Icon icon,
    Object[] selectionValues, Object initialSelectionValue)
    throws HeadlessException {
    JOptionPane    pane = new JOptionPane(message, messageType,
                                          OK_CANCEL_OPTION, icon,
                                          null, null);

    pane.setWantsInput(true);
    pane.setSelectionValues(selectionValues);
    pane.setInitialSelectionValue(initialSelectionValue);
    pane.setComponentOrientation(((parentComponent == null) ?
        getRootFrame() : parentComponent).getComponentOrientation());

    int style = styleFromMessageType(messageType);
    JDialog dialog = pane.createDialog(parentComponent, title, style);

    pane.selectInitialValue();
    dialog.show();
    dialog.dispose();

    Object value = pane.getInputValue();

    if (value == UNINITIALIZED_VALUE) {
        return null;
    }
    return value;
}

Based on your description, it sounds like you could use JOptionPane.showConfirmDialog() to acknowledge that a user has been added.

During your application's think time, you might want to pair a progress bar with a JDialog to let the use know the system is working.

If you post example code, the community members here can probably give you more specific guidance on how to best make use of these components within your application.

Upvotes: 2

Related Questions