1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| package modalite;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
private Modality modality=new Modality(ModalityType.DOCUMENT_MODAL);
public Main(String title, Rectangle bounds) {
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(bounds);
createContent(title, getBounds());
}
private void createContent(String frameName, Rectangle bounds) {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add( createButton("Dialogue modal "+frameName, e-> showDialog(this, "Dialogue modal "+frameName, modality)) );
panel.add( createButton("Dialogue modal sans parent", e-> showTopDialog(this, "Dialogue modal sans parent", modality)) );
panel.add( createModalityRadios(modality) );
add(panel);
}
private Component createModalityRadios(Modality modality) {
JPanel radioButtons = new JPanel();
radioButtons.setBorder(BorderFactory.createTitledBorder("Modalité"));
ButtonGroup buttonGroup = new ButtonGroup();
for(ModalityType type : ModalityType.values()) {
addRadio(type, buttonGroup, radioButtons, modality);
}
return radioButtons;
}
private void addRadio(ModalityType modalityType, ButtonGroup buttonGroup, JPanel panel, Modality modality) {
JRadioButton button = new JRadioButton(modalityType.name());
if ( modality.getModality()==modalityType ) {
button.setSelected(true);
}
button.addActionListener(e-> modality.setModality(modalityType));
buttonGroup.add(button);
panel.add(button);
}
private Component createButton(String label, ActionListener actionListener) {
JButton button = new JButton(label);
button.setAlignmentX(CENTER_ALIGNMENT);
button.addActionListener(actionListener);
return button;
}
private void showTopDialog(Component parent, String name, Modality modality) {
Window frame = SwingUtilities.getWindowAncestor(parent);
openDialog(frame, name, modality).setVisible(true);
}
private void showDialog(Component parent, String name, Modality modality) {
openDialog(parent, name, modality).setVisible(true);
}
private JDialog openDialog(Window parent, String name, Modality modality) {
JDialog dialog = new JDialog(parent, name, modality.getModality());
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
dialog.setContentPane(panel);
Modality localmodality = new Modality(ModalityType.DOCUMENT_MODAL);
panel.add( createButton("OptionPane parent = dialog", e-> showOptionPane(dialog, "parent = dialog")));
panel.add( createButton("OptionPane parent = window", e-> showOptionPane(parent, "parent = window")));
panel.add( createButton("OptionPane parent = null", e-> showOptionPane(null, "parent = null")));
panel.add( createButton("OptionPane via dialog", e-> showDialogOptionPane(null, "via dialog", localmodality)));
panel.add( createModalityRadios(localmodality));
dialog.pack();
dialog.setLocationRelativeTo(parent);
return dialog;
}
private void showOptionPane(Component parent, String title) {
JOptionPane.showConfirmDialog(parent, title);
}
private void showDialogOptionPane(Component parent, String title, Modality modality) {
JOptionPane pane = new JOptionPane(title, JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_CANCEL_OPTION, null,
null, null);
JDialog dialog = openDialog(parent, title, modality);
dialog.setContentPane(pane);
dialog.setVisible(true);
}
private JDialog openDialog(Component parent, String name, Modality modality) {
return openDialog(getWindowParent(parent), name, modality);
}
private Window getWindowParent(Component parent) {
if ( parent==null) {
return null;
}
else if ( parent instanceof Window ) {
return (Window)parent;
}
else {
return SwingUtilities.getWindowAncestor(parent);
}
}
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int height = screenSize.height/2;
int width = screenSize.width/4;
Rectangle bounds = new Rectangle(screenSize.width/2-width, (screenSize.height-height)/2,
width, height);
new Main("Frame 1", bounds).setVisible(true);
bounds.translate(width, 0);
new Main("Frame 2", bounds).setVisible(true);
}
public static class Modality {
private ModalityType modality;
public Modality(ModalityType modality) {
this.modality=modality;
}
public ModalityType getModality() {
return modality;
}
public void setModality(ModalityType modality) {
this.modality = modality;
}
}
} |
Partager