1

i have an application, where I need to work on a frame, while I want to have a dialog open.

So I set the modality to Dialog.ModalityType.MODELESS. While this enables me to interact with the parent JFrame, I cannot use the getValue() anymore on the dialog.

Here is a running minimal example:

package Test;

 import java.awt.Dimension;
 import javax.swing.JDialog;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;

 public class TestModalityDialog {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    JFrame frame = new JFrame();
    frame.setPreferredSize(new Dimension(800,600));
    frame.setVisible(true);
    frame.pack();

    JOptionPane optionPane = new JOptionPane();
    String[] options = new String[]{"Hello"};
    JLabel label1 = new JLabel(
            "Click on a cluster to delete it (needs to be confirmed by pressing the 'Confirm' button.");
    JLabel label2 = new JLabel(
            "Press 'p' to undelete an unconfirmed deletion.");
    Object complexMsg[] = { label1, label2 };
    optionPane.setMessage(complexMsg);
    optionPane.setOptions(options);
    optionPane.setMessageType(JOptionPane.PLAIN_MESSAGE);
    JDialog dialog = optionPane.createDialog(frame,
            "Select undesired clusters");
    //dialog.setModalityType(Dialog.ModalityType.MODELESS); //uncomment this line out
    dialog.setVisible(true);
    dialog.setVisible(false);//must be set to false for Modality to work
    dialog.setVisible(true);

    Object obj = optionPane.getValue();
    int result = -1;
    for (int k = 0; k < options.length; k++) {
        if (options[k].equals(obj)) {
            result = k;
        }
    }

    if (result == 0) {
        System.out.println("Succesful");
    }

}

 }

The system out works here, when you press the button labeled "Hello". Put you cannot interact with the frame behind. If you uncomment

//dialog.setModalityType(Dialog.ModalityType.MODELESS);

this will allow "interaction" (not in this minimal example ofc), but I dont get a system out anymore.

Second thing which I did not expect is that you have to press the button two times in the uncommented version to work.

For help I would be gladful, already tried out the other 3 modalitie values, but the did not work.

Cheers, Buddha

mKorbel
  • 109,525
  • 20
  • 134
  • 319
BuddhaWithBigBelly
  • 345
  • 1
  • 6
  • 15

1 Answers1

2

Communication with a modeless dialog can be accomplished using a PropertyChangeEvent, either an existing one, seen here, of one of you own, seen here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045