Example usage for java.awt.event FocusEvent isTemporary

List of usage examples for java.awt.event FocusEvent isTemporary

Introduction

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

Prototype

public boolean isTemporary() 

Source Link

Document

Identifies the focus change event as temporary or permanent.

Usage

From source file:Main.java

public static void main(String[] a) {
    final JTextField textField = new JTextField(5);

    textField.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
        }//from   www  .  j  a va 2  s  . com

        public void focusLost(FocusEvent e) {
            if (!e.isTemporary()) {
                String content = textField.getText();
                if (!content.equals("a")) {
                    System.out.println("illegal value! " + content);
                    SwingUtilities.invokeLater(new FocusGrabber(textField));
                }
            }
        }
    });
    JFrame frame = new JFrame();
    frame.add(textField, BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setVisible(true);

}

From source file:UsingFocusListener.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField textField = new JTextField("A TextField");
    textField.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
            displayMessage("Focus gained", e);
        }//from w  ww.j  a  va2 s .  c  om

        public void focusLost(FocusEvent e) {
            displayMessage("Focus lost", e);
        }

        void displayMessage(String prefix, FocusEvent e) {
            System.out.println(prefix + (e.isTemporary() ? " (temporary):" : ":")
                    + e.getComponent().getClass().getName() + "; Opposite component: "
                    + (e.getOppositeComponent() != null ? e.getOppositeComponent().getClass().getName()
                            : "null"));
        }

    });

    frame.add(textField, "North");
    frame.add(new JTextField(), "South");
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:FocusTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    FocusListener listener = new FocusListener() {
        public void focusGained(FocusEvent e) {
            dumpInfo(e);//ww w  .  j  ava2s.  c om
        }

        public void focusLost(FocusEvent e) {
            dumpInfo(e);
        }

        private void dumpInfo(FocusEvent e) {
            System.out.println("Source  : " + name(e.getComponent()));
            System.out.println("Opposite : " + name(e.getOppositeComponent()));
            System.out.println("Temporary: " + e.isTemporary());
        }

        private String name(Component c) {
            return (c == null) ? null : c.getName();
        }
    };

    // First
    JPanel panel = new JPanel();
    JLabel label = new JLabel("Label 1: ");
    JTextField text = new JTextField("Type your text", 15);
    text.setName("First");
    text.addFocusListener(listener);
    label.setDisplayedMnemonic(KeyEvent.VK_1);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.NORTH);

    // Second
    panel = new JPanel();
    label = new JLabel("Label 2: ");
    text = new JTextField("14.0", 10);
    text.setName("Second");
    text.addFocusListener(listener);
    text.setHorizontalAlignment(JTextField.RIGHT);
    label.setDisplayedMnemonic(KeyEvent.VK_2);
    label.setLabelFor(text);
    panel.add(label);
    panel.add(text);
    contentPane.add(panel, BorderLayout.SOUTH);

    frame.pack();
    frame.show();
}

From source file:Main.java

public void focusLost(FocusEvent evt) {
    System.out.println("lost the focus.");
    boolean isTemporary = evt.isTemporary();
}

From source file:Main.java

void displayMessage(String prefix, FocusEvent e) {
    System.out.println(prefix + (e.isTemporary() ? " (temporary):" : ":")
            + e.getComponent().getClass().getName() + "; Opposite component: "
            + (e.getOppositeComponent() != null ? e.getOppositeComponent().getClass().getName() : "null"));
}

From source file:Main.java

public void focusLost(FocusEvent evt) {
    final JTextComponent c = (JTextComponent) evt.getSource();
    String s = c.getText();/*from  ww  w. jav  a 2  s . c o m*/

    if (evt.isTemporary()) {
        return;
    }
    for (int i = 0; i < s.length(); i++) {
        if (!Character.isDigit(s.charAt(i))) {
            System.out.println("must only contain digits");
            c.requestFocus();
            break;
        }
    }
}

From source file:Main.java

public void init() {
    tf1 = new JTextField(5);

    tf1.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
        };/* w w  w .  ja  v a2s .co m*/

        public void focusLost(FocusEvent e) {
            if (!e.isTemporary()) {
                String content = tf1.getText();
                if (!content.equals("a")) {
                    System.out.println("illegal value! " + content);
                    SwingUtilities.invokeLater(new FocusGrabber(tf1));
                }
            }
        }
    });
}

From source file:FocusEventDemo.java

void displayMessage(String prefix, FocusEvent e) {
    display.append(prefix + (e.isTemporary() ? " (temporary):" : ":") + e.getComponent().getClass().getName()
            + "; Opposite component: "
            + (e.getOppositeComponent() != null ? e.getOppositeComponent().getClass().getName() : "null")
            + newline);// ww  w . j a  va 2  s.com
    display.setCaretPosition(display.getDocument().getLength());
}

From source file:com.aw.swing.mvp.cmp.pick.PickManager.java

/**
 * @param attrName La caja de texto txt<attrName> debe existir
 * @param pick//from   w ww .  j  a  v a2  s .c o m
 * @return
 */
public Pick registerPick(String attrName, final Pick pick) {
    final String pickName = getPickName(attrName);
    getPicksInfo().add(new PickInfo(pick, attrName, pickName));
    picks.add(pick);
    if (pick instanceof PickImpl) {
        ((PickImpl) pick).setPresenter(presenter);
        pick.setMainAttribute(attrName);
        final JTextComponent jTextComponent = (JTextComponent) presenter.getIpView()
                .getComponent(getTxtPick(attrName));
        jTextComponent.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent e) {

                if (e.isTemporary()) {
                    return;
                }

                // if the pick action is currently executing
                Boolean executingPick = (Boolean) jTextComponent
                        .getClientProperty(BindingComponent.ATTR_EXECUTING_PICK_ACTION);
                if ((executingPick != null) && (executingPick)) {
                    jTextComponent.putClientProperty(BindingComponent.ATTR_EXECUTING_PICK_ACTION, null);
                    //                        return;
                }
                System.out.println("XXX    Focus LOST   2");
                // if the focus will be directed to the pick Button
                Component cmp = e.getOppositeComponent();
                if (cmp instanceof JComponent) {
                    JComponent jComponent = (JComponent) cmp;
                    String actionName = (String) jComponent.getClientProperty(BindingComponent.ATTR_ACTION);
                    if (pickName.equals(actionName)) {
                        return;
                    }
                }

                if (!pick.isPickFilled()) {

                    jTextComponent.setText("");
                }
            }
        });
        jTextComponent.addKeyListener(new PickKeyListener(pick));
        jTextComponent.putClientProperty(PICK_NAME, pickName);
        jTextComponent.putClientProperty(PICK, pick);
        //            JButton jButton = (JButton) presenter.getIpView().getComponent(getBtnPick(attrName));
    }
    return pick;
}

From source file:cz.lidinsky.editor.TableCellEditor.java

public void focusLost(java.awt.event.FocusEvent e) {
    if (!e.isTemporary()) {
        if (!(e.getOppositeComponent() instanceof JTable)) {
            cancelCellEditing();/*from  w  ww .  ja v  a  2 s .  com*/
        }
    }
}