Example usage for java.awt.event FocusEvent getComponent

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

Introduction

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

Prototype

public Component getComponent() 

Source Link

Document

Returns the originator of the event.

Usage

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);/*  w  w w  .j a  va 2 s  .c o  m*/
        }

        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: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 w  w.j  a  v  a  2s.  co m*/

        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: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:facebookchat.ui.common.JNABalloon.java

public JNABalloon(String BALLOON_TEXT, JComponent owner, int X, int Y) {
    //System.setProperty("sun.java2d.noddraw", "true");
    content = new JLabel(BALLOON_TEXT);
    content.setIconTextGap(10);/* w  ww  . ja v a 2 s. c o m*/
    content.setBorder(new EmptyBorder(0, 8, 0, 8));
    content.setSize(content.getPreferredSize());
    content.setIcon(new InfoIcon());
    popup = BalloonManager.getBalloon(owner, content, X, Y);
    content.addMouseListener(listener);
    content.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent arg0) {
            log.debug("content get focusssssssssss");
        }

        public void focusLost(FocusEvent fe) {
            log.debug("content lost focusssssssssss");
            fe.getComponent().removeMouseListener(listener);
            if (popup != null)
                popup.hide();
        }
    });
}

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);/*from  w  w w .  j  a  v a 2 s .c  o m*/
    display.setCaretPosition(display.getDocument().getLength());
}

From source file:TextInputDemo.java

/**
 * Called when one of the fields gets the focus so that
 * we can select the focused field.//from ww w. java2  s.c o m
 */
public void focusGained(FocusEvent e) {
    Component c = e.getComponent();
    if (c instanceof JFormattedTextField) {
        selectItLater(c);
    } else if (c instanceof JTextField) {
        ((JTextField) c).selectAll();
    }
}

From source file:com.dbschools.quickquiz.client.taker.MainWindow.java

/**
 * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
 *//*from  w  w  w.  j  a v a  2  s . c  om*/
public final void focusGained(final FocusEvent e) {
    if (e.getID() == FocusEvent.FOCUS_GAINED && e.getComponent() == txtChatLine) {
        getRootPane().setDefaultButton(btnSendChatLine);
    }

    else if (e.getID() == FocusEvent.FOCUS_GAINED && e.getComponent() == txtAnswer) {
        getRootPane().setDefaultButton(btnSubmit);
    }
}

From source file:de.uka.aifb.com.systemDynamics.gui.ModelExecutionChartPanel.java

/**
 * Performs a gained focus event./*from w w w .  j  a v a2s.c  o  m*/
 * 
 * @param e event
 */
public void focusGained(FocusEvent e) {
    Component c = e.getComponent();
    if (c instanceof JTextField) {
        ((JTextField) c).selectAll();
    }
}

From source file:com.openbravo.pos.ticket.ProductFilter.java

private void m_jNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_m_jNameFocusGained
    if (evt.getComponent() instanceof JTextField) {
        ((JTextField) evt.getComponent()).selectAll();
    }/*from  w w  w.  j  a  v a 2 s  .  c o  m*/
}

From source file:com.openbravo.pos.ticket.ProductFilter.java

private void m_jReferencem_jNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_m_jReferencem_jNameFocusGained
    if (evt.getComponent() instanceof JTextField) {
        ((JTextField) evt.getComponent()).selectAll();
    }/*from  w ww  . j av  a 2  s. c om*/
}