0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jannissary
  • 79
  • 2
  • 8
  • *"Does it cause performance and visualization problems?"* With proper coding, it should make no difference (or so little difference as to not be noticeable). I do not understand "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.". Given either type of dialog can display panels, I cannot understand what you mean by 'customize'. Customize how exactly? BTW - rather than setting the panel visible, it would be better to use a card layout. Ultimately, the choice between a panel 'in place` in the .. – Andrew Thompson Mar 01 '17 at 18:14
  • .. main GUI and in a separate top level container (like a dialog or option pane) comes more down to what the user expects and is comfortable with - the 'user experience'. The differences between a dialog and option pane are largely down to ..the dialog requires more coding, but the option pane is tricky to alter (it is is fine as-is, I'd go with the option pane). – Andrew Thompson Mar 01 '17 at 18:16
  • `JDialog` is just another, blank window, just like `JFrame`, your ability to customise it comes down to you own limitations. `JOptionPane` is highly customisable, but is intended to get information from the user (present buttons for example), so it might not suit your needs for display "progress" messages (unless you want to supply a cancel button) – MadProgrammer Mar 01 '17 at 21:18

1 Answers1

2

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.

Community
  • 1
  • 1
Jason Braucht
  • 2,358
  • 19
  • 31