Example usage for java.awt.event FocusEvent getOppositeComponent

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

Introduction

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

Prototype

public Component getOppositeComponent() 

Source Link

Document

Returns the other Component involved in this focus change.

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  .ja  v  a 2 s.  com
        }

        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 ww  . j  a v  a 2s  .  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:Main.java

public void focusGained(FocusEvent evt) {

    Component c = evt.getOppositeComponent();
    System.out.println(c.getName());
}

From source file:Main.java

public void focusLost(FocusEvent evt) {

    Component c = evt.getOppositeComponent();
    System.out.println(c.getName() + "Opposite Component");
}

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:cz.lidinsky.editor.TableCellEditor.java

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

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

From source file:com.github.fritaly.dualcommander.TabbedPane.java

@Override
public void focusGained(FocusEvent e) {
    // Propagate the event
    final FocusListener[] listeners = getListeners(FocusListener.class);

    if (listeners != null) {
        final FocusEvent event = new FocusEvent(this, e.getID(), e.isTemporary(), e.getOppositeComponent());

        for (FocusListener listener : listeners) {
            listener.focusGained(event);
        }//from  ww  w . j a  v a2s  .c  om
    }
}

From source file:com.github.fritaly.dualcommander.TabbedPane.java

@Override
public void focusLost(FocusEvent e) {
    // Propagate the event
    final FocusListener[] listeners = getListeners(FocusListener.class);

    if (listeners != null) {
        final FocusEvent event = new FocusEvent(this, e.getID(), e.isTemporary(), e.getOppositeComponent());

        for (FocusListener listener : listeners) {
            listener.focusLost(event);//from   ww w .ja  v a 2s .com
        }
    }
}

From source file:de.fhg.iais.asc.ui.parts.HarvesterPanel.java

private JTextField createSetsTextField() {
    final JTextField textField = new JTextField(30);

    textField.addFocusListener(new FocusAdapter() {
        @Override//from   ww  w  .j a v  a 2 s.co  m
        public void focusGained(FocusEvent e) {
            e.getOppositeComponent().requestFocus();

            // get the frame the panel is embedded in
            Component currentComponent = HarvesterPanel.this;
            while (currentComponent.getParent() != null && !(currentComponent instanceof JFrame)) {
                currentComponent = currentComponent.getParent();
            }
            final JFrame parentFrame = currentComponent instanceof JFrame ? (JFrame) currentComponent : null;

            if (HarvesterPanel.this.availableSets == null) {
                LocalizedOptionPane.showMessageDialog(parentFrame, "No_setnames_retrieved",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }

            String[] selectedSets = textField.getText().split(","); //$NON-NLS-1$

            for (int i = 0; i < selectedSets.length; i++) {
                selectedSets[i] = selectedSets[i].trim();
            }

            SetSelectionWindow ssw = new SetSelectionWindow(parentFrame, HarvesterPanel.this.availableSets,
                    selectedSets);
            ssw.setVisible(true);

            textField.setText(StringUtils.join(ssw.getSelectedSets(), ", "));
        }
    });

    return textField;
}