Search in sources :

Example 1 with WindowFocusListener

use of java.awt.event.WindowFocusListener in project libgdx by libgdx.

the class JglfwInput method getTextInput.

public void getTextInput(final TextInputListener listener, final String title, final String text, final String hint) {
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            JPanel panel = new JPanel(new FlowLayout());
            JPanel textPanel = new JPanel() {

                public boolean isOptimizedDrawingEnabled() {
                    return false;
                }

                ;
            };
            textPanel.setLayout(new OverlayLayout(textPanel));
            panel.add(textPanel);
            final JTextField textField = new JTextField(20);
            textField.setText(text);
            textField.setAlignmentX(0.0f);
            textPanel.add(textField);
            final JLabel placeholderLabel = new JLabel(hint);
            placeholderLabel.setForeground(Color.GRAY);
            placeholderLabel.setAlignmentX(0.0f);
            textPanel.add(placeholderLabel, 0);
            textField.getDocument().addDocumentListener(new DocumentListener() {

                public void removeUpdate(DocumentEvent event) {
                    this.updated();
                }

                public void insertUpdate(DocumentEvent event) {
                    this.updated();
                }

                public void changedUpdate(DocumentEvent event) {
                    this.updated();
                }

                private void updated() {
                    placeholderLabel.setVisible(textField.getText().length() == 0);
                }
            });
            JOptionPane pane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, null);
            pane.setComponentOrientation(JOptionPane.getRootFrame().getComponentOrientation());
            pane.selectInitialValue();
            placeholderLabel.setBorder(new EmptyBorder(textField.getBorder().getBorderInsets(textField)));
            JDialog dialog = pane.createDialog(null, title);
            dialog.addWindowFocusListener(new WindowFocusListener() {

                public void windowLostFocus(WindowEvent arg0) {
                }

                public void windowGainedFocus(WindowEvent arg0) {
                    textField.requestFocusInWindow();
                }
            });
            dialog.setVisible(true);
            dialog.dispose();
            Object selectedValue = pane.getValue();
            if (selectedValue != null && (selectedValue instanceof Integer) && (Integer) selectedValue == JOptionPane.OK_OPTION)
                listener.input(textField.getText());
            else
                listener.canceled();
        }
    });
}
Also used : JPanel(javax.swing.JPanel) DocumentListener(javax.swing.event.DocumentListener) FlowLayout(java.awt.FlowLayout) JLabel(javax.swing.JLabel) JTextField(javax.swing.JTextField) DocumentEvent(javax.swing.event.DocumentEvent) JOptionPane(javax.swing.JOptionPane) WindowEvent(java.awt.event.WindowEvent) OverlayLayout(javax.swing.OverlayLayout) EmptyBorder(javax.swing.border.EmptyBorder) JDialog(javax.swing.JDialog) WindowFocusListener(java.awt.event.WindowFocusListener)

Example 2 with WindowFocusListener

use of java.awt.event.WindowFocusListener in project screenbird by adamhub.

the class PreviewPlayerForm method initView.

/**
     * Initializes the preview player view.
     */
private void initView() {
    this.setVisible(false);
    if (this.jfPreviewPlayer != null) {
        this.jfPreviewPlayer.dispose();
    }
    this.jfPreviewPlayer = new JFrame("Screenbird PreviewPlayer");
    this.jfPreviewPlayer.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(ResourceUtil.LOGO_TASKBAR)));
    this.jpPreviewPlayer = new PreviewPlayer(this, this.scrubManager);
    this.jfPreviewPlayer.setUndecorated(true);
    this.jfPreviewPlayer.add(this.jpPreviewPlayer);
    try {
        if (!AWTUtilities.isTranslucencyCapable(this.getGraphicsConfiguration())) {
            log("Can not set transparency for Preview Player");
            this.setBackground(new Color(64, 64, 64, 255));
            this.jpPreviewPlayer.setBackground(new Color(64, 64, 64, 255));
            this.jpPreviewPlayer.setOpaque(true);
        } else {
            log("Transparency is set for Preview Player");
            AWTUtilities.setWindowOpaque(this.jfPreviewPlayer, false);
        }
    } catch (Exception ex) {
        log(ex);
    }
    // Hack for handling draggable JFrames on Mac OSX
    this.jfPreviewPlayer.getRootPane().putClientProperty("apple.awt.draggableWindowBackground", Boolean.FALSE);
    this.jfPreviewPlayer.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.jfPreviewPlayer.setResizable(true);
    this.jfPreviewPlayer.pack();
    this.jfPreviewPlayer.setVisible(false);
    // Window focus listener for synchronizing display of RecordFromHere 
    // panel with the PreviewPlayer
    this.jfPreviewPlayer.addWindowFocusListener(new WindowFocusListener() {

        public void windowGainedFocus(WindowEvent we) {
            jpPreviewPlayer.giveFocusToRecordFromHerePanel();
        }

        public void windowLostFocus(WindowEvent we) {
            jpPreviewPlayer.takeFocusToRecordFromHerePanel();
        }
    });
}
Also used : JFrame(javax.swing.JFrame) Color(java.awt.Color) WindowEvent(java.awt.event.WindowEvent) WindowFocusListener(java.awt.event.WindowFocusListener)

Example 3 with WindowFocusListener

use of java.awt.event.WindowFocusListener in project jdk8u_jdk by JetBrains.

the class TranserFocusToWindow method main.

public static void main(String[] args) {
    Robot robot = Util.createRobot();
    Frame owner_frame = new Frame("Owner frame");
    owner_frame.setBounds(0, 0, 200, 200);
    owner_frame.setVisible(true);
    Util.waitForIdle(robot);
    Window window = new Window(owner_frame);
    Button btn1 = new Button("button for focus");
    window.add(btn1);
    window.pack();
    window.setLocation(0, 300);
    window.setVisible(true);
    Util.waitForIdle(robot);
    Frame another_frame = new Frame("Another frame");
    Button btn2 = new Button("button in a frame");
    another_frame.add(btn2);
    another_frame.pack();
    another_frame.setLocation(300, 0);
    another_frame.setVisible(true);
    Util.waitForIdle(robot);
    Util.clickOnTitle(owner_frame, robot);
    Util.waitForIdle(robot);
    setFocus(btn1, robot);
    setFocus(btn2, robot);
    owner_frame.addWindowFocusListener(new WindowFocusListener() {

        public void windowLostFocus(WindowEvent we) {
            System.out.println(we);
        }

        public void windowGainedFocus(WindowEvent we) {
            System.out.println(we);
            throw new RuntimeException("owner frame must not receive WINDWO_GAINED_FOCUS");
        }
    });
    window.addWindowFocusListener(new WindowFocusListener() {

        public void windowLostFocus(WindowEvent we) {
            System.out.println(we);
        }

        public void windowGainedFocus(WindowEvent we) {
            System.out.println(we);
        }
    });
    another_frame.addWindowFocusListener(new WindowFocusListener() {

        public void windowLostFocus(WindowEvent we) {
            System.out.println(we);
        }

        public void windowGainedFocus(WindowEvent we) {
            System.out.println(we);
        }
    });
    // we need this delay so WM can not treat two clicks on title as double click
    robot.delay(500);
    Util.clickOnTitle(owner_frame, robot);
    Util.waitForIdle(robot);
    System.out.println("test passed");
}
Also used : Window(java.awt.Window) Frame(java.awt.Frame) Button(java.awt.Button) WindowEvent(java.awt.event.WindowEvent) Robot(java.awt.Robot) WindowFocusListener(java.awt.event.WindowFocusListener)

Aggregations

WindowEvent (java.awt.event.WindowEvent)3 WindowFocusListener (java.awt.event.WindowFocusListener)3 Button (java.awt.Button)1 Color (java.awt.Color)1 FlowLayout (java.awt.FlowLayout)1 Frame (java.awt.Frame)1 Robot (java.awt.Robot)1 Window (java.awt.Window)1 JDialog (javax.swing.JDialog)1 JFrame (javax.swing.JFrame)1 JLabel (javax.swing.JLabel)1 JOptionPane (javax.swing.JOptionPane)1 JPanel (javax.swing.JPanel)1 JTextField (javax.swing.JTextField)1 OverlayLayout (javax.swing.OverlayLayout)1 EmptyBorder (javax.swing.border.EmptyBorder)1 DocumentEvent (javax.swing.event.DocumentEvent)1 DocumentListener (javax.swing.event.DocumentListener)1