ダイアログを開きたいときに、フレームで作業する必要があるアプリケーションがあります。
そこで、モダリティを に設定しましたDialog.ModalityType.MODELESS
。これにより、親 JFrame と対話できるようになりますgetValue()
が、ダイアログで を使用できなくなりました。
実行中の最小限の例を次に示します。
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");
}
}
}
「Hello」というラベルの付いたボタンを押すと、ここでシステムが機能します。後ろのフレームとやり取りすることはできません。コメントを外すと
//dialog.setModalityType(Dialog.ModalityType.MODELESS);
これにより、「相互作用」が可能になります (この最小の例ではありません)。
私が予想していなかった 2 番目のことは、コメントなしのバージョンでボタンを 2 回押す必要があることです。
他の 3 つのモダリティ値を既に試しましたが、うまくいきませんでした。
乾杯、ブッダ