Java Tutorial/Swing/Modality — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 15:33, 31 мая 2010
Содержание
Demonstrating Modality Types
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DualModal {
public static void main(String args[]) {
final JFrame frame1 = new JFrame("Left");
final JFrame frame2 = new JFrame("Right");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton("Left");
JButton button2 = new JButton("Right");
frame1.add(button1);
frame2.add(button2);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
JOptionPane pane = new JOptionPane("New label", JOptionPane.QUESTION_MESSAGE);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(frame2, "Enter Text");
// dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
dialog.setVisible(true);
String text = (String) pane.getInputValue();
if (!JOptionPane.UNINITIALIZED_VALUE.equals(text) && text.trim().length() > 0) {
source.setText(text);
}
}
};
button1.addActionListener(listener);
button2.addActionListener(listener);
frame1.setBounds(100, 100, 200, 200);
frame1.setVisible(true);
frame2.setBounds(400, 100, 200, 200);
frame2.setVisible(true);
}
}
Dialog.ModalityType.DOCUMENT_MODAL
import java.awt.Dialog;
import java.awt.FlowLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class DocumentModalDialogDemo {
public static void main(String[] args) {
final JFrame parent = new JFrame("Parent Frame");
parent.setLayout(new FlowLayout());
parent.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
parent.setBounds(100, 100, 300, 200);
parent.setVisible(true);
JDialog dialog1 = new JDialog(parent, "Dialog1 - Modeless Dialog");
dialog1.setBounds(200, 200, 300, 200);
dialog1.setVisible(true);
JDialog dialog2 = new JDialog(parent, "Dialog2 - Document-Modal Dialog",
Dialog.ModalityType.DOCUMENT_MODAL);
dialog2.setBounds(300, 300, 300, 200);
JDialog dialog3 = new JDialog(dialog2, "Dialog3 - Modeless Dialog");
dialog3.setBounds(400, 400, 300, 200);
dialog3.setVisible(true);
dialog2.setVisible(true);
}
}
Four new AWT modality models introduced with the Java Mustang release.
Modal TypeDescriptionMode-lessDoes not block any other window while it"s rendered on the user display.Document-modalBlocks all windows from the same document with the exception of those windows from its child hierarchy.Application-modalBlocks all windows from the same application with the exception of those windows from its child hierarchy.Toolkit-modalBlocks all windows from the same toolkit with the exception of those toolkits from its child hierarchy.
Improved Dialog Modality
Mustang introduces different types of modality, in which you no longer use setModal but the new method setModalityType in the Dialog class to make a dialog modal.
Dialog.ModalityType is an enum in the java.awt package.
Its values are as follows:
- APPLICATION_MODAL. Blocks all windows in the same application except those with the modal dialog as their owner.
- DOCUMENT_MODAL. Makes all windows from the same document inaccessible, except those from the modal dialog"s child hierarchy.
- MODELESS. Does not block any other windows.
- TOOLKIT_MODAL. Makes all windows from the same toolkit inaccessible, except those from the modal dialog"s child hierarchy.
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class ApplicationModalDialogDemo {
public static void main(String[] args) {
final JFrame parent1 = new JFrame("Parent Frame 1");
parent1.setLayout(new FlowLayout());
parent1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Application modal dialog");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(parent1, "Application-Modal Dialog",
Dialog.ModalityType.APPLICATION_MODAL);
dialog.setBounds(200, 150, 200, 150);
dialog.setVisible(true);
}
});
parent1.add(button);
parent1.setBounds(100, 100, 200, 150);
parent1.setVisible(true);
JFrame parent2 = new JFrame("Parent Frame 2");
parent2.setBounds(500, 100, 200, 150);
parent2.setVisible(true);
}
}
Modeless Dialog
import java.awt.Dialog;
import java.awt.FlowLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class DocumentModalDialogDemo {
public static void main(String[] args) {
final JFrame parent = new JFrame("Parent Frame");
parent.setLayout(new FlowLayout());
parent.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
parent.setBounds(100, 100, 300, 200);
parent.setVisible(true);
JDialog dialog1 = new JDialog(parent, "Dialog1 - Modeless Dialog");
dialog1.setBounds(200, 200, 300, 200);
dialog1.setVisible(true);
JDialog dialog2 = new JDialog(parent, "Dialog2 - Document-Modal Dialog",
Dialog.ModalityType.DOCUMENT_MODAL);
dialog2.setBounds(300, 300, 300, 200);
JDialog dialog3 = new JDialog(dialog2, "Dialog3 - Modeless Dialog");
dialog3.setBounds(400, 400, 300, 200);
dialog3.setVisible(true);
dialog2.setVisible(true);
}
}
Using modality exclusion
import java.awt.Dialog;
import java.awt.FlowLayout;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class ApplicationModalDialogWithExcludeDemo {
public static void main(String[] args) {
final JFrame parent1 = new JFrame("Parent Frame 1");
parent1.setLayout(new FlowLayout());
parent1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
parent1.setBounds(100, 100, 200, 150);
parent1.setVisible(true);
JFrame parent2 = new JFrame("Parent Frame 2");
parent2.setBounds(500, 100, 300, 150);
parent2.setVisible(true);
JFrame parent3 = new JFrame("Parent Frame 3 - Excluded");
parent3.setBounds(300, 400, 300, 150);
parent3.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
parent3.setVisible(true);
JDialog dialog = new JDialog(parent1, "Application-Modal Dialog",
Dialog.ModalityType.APPLICATION_MODAL);
dialog.setBounds(300, 200, 300, 150);
dialog.setVisible(true);
}
}