fubarable

dialog w keylistener

Nov 4th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.Dialog.ModalityType;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class Foo01 {
  7.     public static void main(String[] args) {
  8.         SwingUtilities.invokeLater(() -> foo());
  9.     }
  10.    
  11.     public static void foo() {
  12.         JPanel panel = new JPanel();
  13.         JButton button = new JButton("Button");
  14.         panel.add(button);
  15.        
  16.         final JFrame frame = new JFrame("Frame");
  17.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.         frame.add(panel);
  19.         frame.pack();
  20.         frame.setLocationByPlatform(true);
  21.         frame.setVisible(true);
  22.        
  23.         button.addActionListener(e -> {
  24.             JPanel panel2 = new JPanel();
  25.             panel2.setPreferredSize(new Dimension(100, 40));
  26.             JDialog dialog = new JDialog(frame, "Dialog", ModalityType.APPLICATION_MODAL);
  27.             dialog.add(panel2);
  28.             dialog.pack();
  29.             dialog.setLocationByPlatform(true);
  30.             dialog.addKeyListener(new KeyAdapter() {
  31.                 @Override
  32.                 public void keyTyped(KeyEvent e) {
  33.                     char ch=e.getKeyChar();
  34.                     if (ch==' ') {
  35.                         System.out.println("got space");
  36.                     }
  37.                 }
  38.             });            
  39.             dialog.setVisible(true);            
  40.         });
  41.     }
  42. }
Add Comment
Please, Sign In to add comment