import java.awt.BorderLayout; import java.awt.Dialog; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.reflect.InvocationTargetException; import java.util.Locale; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class ModalDialogTest16 { private static JDialog d = null; private static String command = ""; public ModalDialogTest16 () { } /** Logs OS and JVM info */ public static void logSystemInfo () { System.out.println("OS Info: " + System.getProperty("os.name","Unknown") + " version " + System.getProperty("os.version","unknown") + " running on " + System.getProperty("os.arch","unknown")); System.out.println("JVM Info: " + System.getProperty("java.version","Unknown") + "; " + System.getProperty("java.vm.name","unknown") + " " + System.getProperty("java.vm.version","unknown") + "; " + System.getProperty("java.vendor","unknown")); System.out.println("Java Home: " + System.getProperty("java.home","Unknown")); System.out.println("System Locale: " + Locale.getDefault().toString()); } public static void main(String[] args) { logSystemInfo(); System.out.println(""); try { SwingUtilities.invokeAndWait(new Runnable () { public void run () { setDefaultLookAndFeel(); System.out.println("Look And Feel: " + UIManager.getLookAndFeel().getClass().getName()); JButton yesButton = new JButton("Yes"); JButton noButton = new JButton("No"); ActionListener listener = new ActionListener () { public void actionPerformed (ActionEvent e) { command = e.getActionCommand(); d.setVisible(false); } }; yesButton.addActionListener(listener); noButton.addActionListener(listener); yesButton.setActionCommand("yes"); // NOI18N noButton.setActionCommand("no"); // NOI18N d = new JDialog((Window) null,"Test Modal Dialog",Dialog.ModalityType.APPLICATION_MODAL); //d.getContentPane().add(licensePanel,BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); buttonPanel.setBorder(BorderFactory.createEmptyBorder(17,12,11,11)); buttonPanel.add(yesButton); buttonPanel.add(noButton); d.getContentPane().add(buttonPanel,BorderLayout.SOUTH); d.setSize(new Dimension(600,600)); d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); d.setModal(true); d.setResizable(true); //Center on screen d.setLocationRelativeTo(null); d.setVisible(true); System.exit(0); } }); } catch (InterruptedException exc) { exc.printStackTrace(); } catch (InvocationTargetException exc) { exc.printStackTrace(); } } /** Tries to set default L&F according to platform. * Uses: * Metal L&F on Linux and Solaris * Windows L&F on Windows * Aqua L&F on Mac OS X * System L&F on other OS */ public static void setDefaultLookAndFeel () { String uiClassName; if (isWindowsOS()) { uiClassName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; //NOI18N } else if (isMacOSX()) { uiClassName = "apple.laf.AquaLookAndFeel"; //NOI18N } else if (isLinuxOS() || isSunOS()) { uiClassName = "javax.swing.plaf.metal.MetalLookAndFeel"; //NOI18N } else { uiClassName = UIManager.getSystemLookAndFeelClassName(); } if (uiClassName.equals(UIManager.getLookAndFeel().getClass().getName())) { //Desired L&F is already set return; } try { UIManager.setLookAndFeel(uiClassName); } catch (Exception ex) { System.err.println("Cannot set L&F " + uiClassName); //NOI18N System.err.println("Exception:" + ex.getMessage()); //NOI18N ex.printStackTrace(); } } private static boolean isWindowsOS() { return System.getProperty("os.name").startsWith("Windows"); //NOI18N } private static boolean isLinuxOS() { return System.getProperty("os.name").startsWith("Lin"); //NOI18N } private static boolean isSunOS() { return System.getProperty("os.name").startsWith("Sun"); //NOI18N } private static boolean isMacOSX() { return System.getProperty("os.name").startsWith("Mac OS X"); //NOI18N } }