Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Dimension;
- import java.awt.Dialog.ModalityType;
- import java.awt.event.*;
- import javax.swing.*;
- public class Foo01 {
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> foo());
- }
- public static void foo() {
- JPanel panel = new JPanel();
- JButton button = new JButton("Button");
- panel.add(button);
- final JFrame frame = new JFrame("Frame");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(panel);
- frame.pack();
- frame.setLocationByPlatform(true);
- frame.setVisible(true);
- button.addActionListener(e -> {
- JPanel panel2 = new JPanel();
- panel2.setPreferredSize(new Dimension(100, 40));
- JDialog dialog = new JDialog(frame, "Dialog", ModalityType.APPLICATION_MODAL);
- dialog.add(panel2);
- dialog.pack();
- dialog.setLocationByPlatform(true);
- dialog.addKeyListener(new KeyAdapter() {
- @Override
- public void keyTyped(KeyEvent e) {
- char ch=e.getKeyChar();
- if (ch==' ') {
- System.out.println("got space");
- }
- }
- });
- dialog.setVisible(true);
- });
- }
- }
Add Comment
Please, Sign In to add comment