Advertisement
Guest User

Modeless dialog test

a guest
Dec 3rd, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. package modeless;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.FlowLayout;
  5. import java.awt.Window;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JDialog;
  13. import javax.swing.JFrame;
  14. import javax.swing.JTextArea;
  15. import javax.swing.SwingUtilities;
  16.  
  17. public class ModelessTest extends JFrame {
  18.  
  19.     private static int dialogCount = 0;
  20.  
  21.     private JTextArea taMessages = new JTextArea(10, 0);
  22.  
  23.     public ModelessTest() {
  24.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  25.         setTitle("Modeless dialogs with callbacks");
  26.  
  27.         setLayout(new BorderLayout());
  28.  
  29.         taMessages.setEditable(false);
  30.         add(taMessages, BorderLayout.CENTER);
  31.  
  32.         JButton btnSpawnModeless = new JButton("Spawn modeless dialog");
  33.         btnSpawnModeless.addActionListener(new ActionListener() {
  34.  
  35.             @Override
  36.             public void actionPerformed(ActionEvent e) {
  37.                 ++dialogCount;
  38.                 final String dialogTitle = "Dialog #" + dialogCount;
  39.                 DialogCallback callback = new DialogCallback() {
  40.  
  41.                     @Override
  42.                     public void ok() {
  43.                         taMessages.append("OK: " + dialogTitle + "\n");
  44.                     }
  45.  
  46.                     @Override
  47.                     public void cancel() {
  48.                         taMessages.append("Cancel: " + dialogTitle + "\n");
  49.                     }
  50.                 };
  51.                 MyModelessDialog dialog = new MyModelessDialog(ModelessTest.this, dialogTitle, callback);
  52.                 dialog.setVisible(true);
  53.             }
  54.         });
  55.         add(btnSpawnModeless, BorderLayout.SOUTH);
  56.         pack();
  57.     }
  58.  
  59.     public static void main(String[] args) {
  60.         SwingUtilities.invokeLater(new Runnable() {
  61.  
  62.             @Override
  63.             public void run() {
  64.                 new ModelessTest().setVisible(true);
  65.             }
  66.         });
  67.     }
  68.  
  69. }
  70.  
  71. class MyModelessDialog extends JDialog {
  72.     private final DialogCallback cbk;
  73.     private JButton okButton, cancelButton;
  74.  
  75.     public MyModelessDialog(Window owner, String title, DialogCallback callback) {
  76.         super(owner, title);
  77.         cbk = callback;
  78.         setModalityType(ModalityType.MODELESS);
  79.  
  80.         createLayout();
  81.  
  82.         okButton.addActionListener(new ActionListener() {
  83.             public void actionPerformed(ActionEvent e) {
  84.                 onOK();
  85.             }
  86.         });
  87.  
  88.         cancelButton.addActionListener(new ActionListener() {
  89.             public void actionPerformed(ActionEvent e) {
  90.                 onCancel();
  91.             }
  92.         });
  93.  
  94.         // Treat closing the dialog the same as pressing "Cancel":
  95.         addWindowListener(new WindowAdapter() {
  96.             public void windowClosing(WindowEvent e) {
  97.                 onCancel();
  98.             }
  99.         });
  100.     }
  101.  
  102.     private void createLayout() {
  103.         setLayout(new FlowLayout());
  104.  
  105.         okButton = new JButton("OK");
  106.         add(okButton);
  107.  
  108.         cancelButton = new JButton("Cancel");
  109.         add(cancelButton);
  110.  
  111.         pack();
  112.     }
  113.  
  114.     private void onOK() {
  115.         cbk.ok();
  116.         setVisible(false);
  117.     }
  118.  
  119.     private void onCancel() {
  120.         cbk.cancel();
  121.         setVisible(false);
  122.     }
  123. }
  124.  
  125.  
  126. interface DialogCallback {
  127.     void ok();
  128.  
  129.     void cancel();
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement