Example usage for java.awt.event FocusAdapter FocusAdapter

List of usage examples for java.awt.event FocusAdapter FocusAdapter

Introduction

In this page you can find the example usage for java.awt.event FocusAdapter FocusAdapter.

Prototype

FocusAdapter

Source Link

Usage

From source file:Main.java

public static void main(final String[] args) {
    JFrame frame = new JFrame("Frame");
    JDialog dialog = new JDialog(frame, "Dialog");
    frame.add(new JLabel("Content"));
    frame.addMouseListener(new MouseAdapter() {
        @Override/*from   w  w w. ja  va 2 s.c  o m*/
        public void mousePressed(MouseEvent arg0) {
            System.out.println("frame pressed");
            System.out.println("dialog focused " + dialog.isFocused());
            System.out.println("frame focused " + frame.isFocused());
            super.mousePressed(arg0);
        }
    });
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    dialog.add(new JLabel("Content"));
    dialog.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent arg0) {
            super.focusLost(arg0);
            dialog.requestFocus();
        }
    });
    dialog.pack();
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);
}

From source file:Main.java

public static void focusSelect(final JTextComponent tf) {
    tf.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent e) {
            tf.selectAll();//  www  .  j a va 2s.  c om
        }
    });
}

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 *
 * @param spinner/*w w w .  j  a va 2 s .com*/
 *            The spinner to fix
 */

public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = (JTextComponent) e.getSource();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}

From source file:Main.java

/**
 * Installs a workaround for bug #4699955 in a JSpinner.
 * //from   ww  w  .  j  a  va2s  .co  m
 * @param spinner
 *            The spinner to fix
 */
public static void installSpinnerBugWorkaround(final JSpinner spinner) {
    ((DefaultEditor) spinner.getEditor()).getTextField().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JTextComponent) {
                final JTextComponent text = ((JTextComponent) e.getSource());
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.selectAll();
                    }
                });
            }
        }
    });
    spinner.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(final FocusEvent e) {
            if (e.getSource() instanceof JSpinner) {
                final JTextComponent text = ((DefaultEditor) ((JSpinner) e.getSource()).getEditor())
                        .getTextField();
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        text.requestFocus();
                    }
                });
            }
        }
    });
}

From source file:Main.java

private FocusListener getFocusListener() {
    return new FocusAdapter() {
        @Override/*  w  ww  .  ja  v a  2s .com*/
        public void focusGained(FocusEvent arg0) {
            super.focusGained(arg0);
            box.setBackground(Color.BLUE);
            validate();
        }

        @Override
        public void focusLost(FocusEvent arg0) {
            super.focusLost(arg0);
            box.setBackground(Color.red);
            validate();
        }
    };
}

From source file:Main.java

private JFormattedTextField init(JFormattedTextField jtf) {
    jtf.setValue(0);//w  w  w  .  j  a v  a2 s .  c  om
    jtf.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            Number v1 = (Number) a.getValue();
            Number v2 = (Number) b.getValue();
            sum.setValue(v1.longValue() + v2.longValue());
        }
    });
    return jtf;
}

From source file:Main.java

/**
 * Set the button to have simplified UI.
 * /*  ww w .  ja  va  2  s . c om*/
 * @param button
 *            button to be modified
 * @param <T>
 *            type of button
 * @return button
 */
public static <T extends AbstractButton> T decoratedToSimpleButton(final T button) {

    button.setForeground(Color.BLACK);
    button.setBackground(Color.LIGHT_GRAY);
    button.setBorderPainted(true);
    button.setFocusPainted(true);
    button.setContentAreaFilled(false);
    button.setOpaque(true);

    if (button instanceof JToggleButton) {
        ((JToggleButton) button).addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (button.isSelected()) {
                    button.setBackground(Color.WHITE);
                }
            }
        });
    }
    button.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent e) {
            super.mouseEntered(e);
            button.setBackground(Color.WHITE);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            super.mouseExited(e);
            if (!button.isSelected()) {
                button.setBackground(Color.LIGHT_GRAY);
            }
        }
    });

    button.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            if (!button.isSelected()) {
                button.setBackground(Color.LIGHT_GRAY);
            }
        }
    });
    Border line = new LineBorder(Color.BLACK);
    Border margin = new EmptyBorder(5, 15, 5, 15);
    Border compound = new CompoundBorder(line, margin);
    button.setBorder(compound);
    return button;
}

From source file:hr.fer.zemris.vhdllab.platform.ui.wizard.AbstractMultiValidationForm.java

@Override
protected JComponent createFormControl() {
    TableFormBuilder builder = new TableFormBuilder(getBindingFactory());
    doBuildForm(builder);/* ww w.  jav  a2s.c  om*/

    JComponent control = builder.getForm();
    control.addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent e) {
            if (componentToGiveFocusTo != null) {
                componentToGiveFocusTo.requestFocusInWindow();
            }
        }
    });
    return control;
}

From source file:org.jspresso.framework.binding.swing.JTextComponentConnector.java

/**
 * {@inheritDoc}/*ww  w  .j a v a 2 s .  c o  m*/
 */
@Override
protected void bindJComponent() {
    getConnectedJComponent().addFocusListener(new FocusAdapter() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void focusLost(FocusEvent e) {
            if (!e.isTemporary()) {
                fireConnectorValueChange();
            }
        }
    });
}

From source file:OAT.ui.AbstractChartFrame.java

private void initComponent() {
    setJMenuBar(new MainMenuBar(this));

    chartPanel.setPreferredSize(getChartPanelSize());
    setContentPane(chartPanel);//  w w w .ja va  2  s . c  o  m

    setDefaultCloseOperation(
            disposeOnClose() ? WindowConstants.DISPOSE_ON_CLOSE : WindowConstants.HIDE_ON_CLOSE);

    if (hideWhenFocusLost()) {
        addFocusListener(new FocusAdapter() {

            @Override
            public void focusLost(FocusEvent e) {
                setVisible(false);
            }
        });
    }

    init();

    //pack and display
    pack();
}