Skip to content

Instantly share code, notes, and snippets.

@renelink
Created June 10, 2025 12:13
Show Gist options
  • Save renelink/3eb86428a194fd16d8a25f51d7c3cb68 to your computer and use it in GitHub Desktop.
Save renelink/3eb86428a194fd16d8a25f51d7c3cb68 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.event.*;
public class ModalityDemo {
// First document (red): frame, modeless dialog box,
// document-modal dialog box
private static Frame f1;
private static Dialog d11;
private static Dialog d12;
// Second document (blue): frame, modeless dialog box,
// document-modal dialog box
private static Frame f2;
private static Dialog d21;
private static Dialog d22;
// Third document (green): modal excluded frame
private static Frame f3;
// Fourth document (grey): frame, file dialog box
// application-modal dialog box
private static Frame f4;
private static FileDialog fd4;
private static WindowListener closeWindow = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
}
};
public static void main(String[] args) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
int sw = gc.getBounds().width - 64;
int sh = gc.getBounds().height - 64;
Label l;
Button b;
MenuBar mb;
Menu m;
MenuItem mi;
Font labelFont = new Font("Tahoma", 24, Font.PLAIN);
// First document
f1 = new Frame("Parent Frame");
f1.setBounds(32, 32, 300, 200);
f1.addWindowListener(closeWindow);
mb = new MenuBar();
m = new Menu("Test");
mi = new MenuItem("Show modeless");
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d11.setVisible(true);
}
});
m.add(mi);
mb.add(m);
f1.setMenuBar(mb);
f1.setLayout(new BorderLayout());
l = new Label("FRAME");
l.setAlignment(Label.CENTER);
l.setFont(labelFont);
l.setBackground(Color.RED);
f1.add(l, BorderLayout.CENTER);
f1.setVisible(true);
d11 = new Dialog(f1, "Modeless Dialog");
// An old constructor. Because the flag "modal" is
// missed, the dialog box is modeless.
d11.setBounds(132, 132, 300, 200);
d11.addWindowListener(closeWindow);
d11.setLayout(new BorderLayout());
l = new Label("MODELESS");
l.setAlignment(Label.CENTER);
l.setFont(labelFont);
l.setBackground(Color.RED);
d11.add(l, BorderLayout.CENTER);
b = new Button("Show document-modal");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d12.setVisible(true);
}
});
d11.add(b, BorderLayout.SOUTH);
d12 = new Dialog(
(Window) d11, "Document-modal Dialog",
Dialog.ModalityType.DOCUMENT_MODAL);
// New constructor with parameter for its modality type
// Parameter type is enum Dialog.ModalityType.
d12.setBounds(232, 232, 300, 200);
d12.addWindowListener(closeWindow);
d12.setLayout(new BorderLayout());
l = new Label("DOCUMENT_MODAL");
l.setAlignment(Label.CENTER);
l.setFont(labelFont);
l.setBackground(Color.RED);
d12.add(l, BorderLayout.CENTER);
// Second document
f2 = new Frame("Parent Frame");
f2.setBounds(sw - 300 + 32, 32, 300, 200);
f2.addWindowListener(closeWindow);
mb = new MenuBar();
m = new Menu("Test");
mi = new MenuItem("Show modeless");
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d21.setVisible(true);
}
});
m.add(mi);
mb.add(m);
f2.setMenuBar(mb);
f2.setLayout(new BorderLayout());
l = new Label("FRAME");
l.setBackground(Color.BLUE);
l.setAlignment(Label.CENTER);
l.setFont(labelFont);
f2.add(l, BorderLayout.CENTER);
f2.setVisible(true);
d21 = new Dialog(f2, "Modeless Dialog");
d21.setBounds(sw - 400 + 32, 132, 300, 200);
d21.addWindowListener(closeWindow);
d21.setLayout(new BorderLayout());
l = new Label("MODELESS");
l.setBackground(Color.BLUE);
l.setAlignment(Label.CENTER);
l.setFont(labelFont);
d21.add(l, BorderLayout.CENTER);
b = new Button("Show document-modal");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Rectangle bounds = d21.getBounds();
bounds.x -= 70;
bounds.y += 70;
d22.setBounds(bounds);
d22.setVisible(true);
}
});
d21.add(b, BorderLayout.SOUTH);
d22 = new Dialog(
(Window) d21, "Document-modal Dialog",
Dialog.ModalityType.DOCUMENT_MODAL);
l = new Label("DOCUMENT_MODAL");
l.setAlignment(Label.CENTER);
l.setFont(labelFont);
l.setBackground(Color.BLUE);
d22.add(l, BorderLayout.CENTER);
d22.addWindowListener(closeWindow);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment