Use Focus Events in Swing : Focus Event « Event « Java






Use Focus Events in Swing

  
 
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class Main extends JPanel implements FocusListener {
  public Main() {
    JTextField textField = new JTextField("A TextField");
    textField.addFocusListener(this);
    JLabel label = new JLabel("A Label");
    label.addFocusListener(this);
    add(label);

    String comboPrefix = "ComboBox Item #";
    final int numItems = 15;
    Vector vector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
      vector.addElement(comboPrefix + i);
    }
    JComboBox comboBox = new JComboBox(vector);
    comboBox.addFocusListener(this);
    add(comboBox);

    JButton button = new JButton("A Button");
    button.addFocusListener(this);
    add(button);

    String listPrefix = "List Item #";
    Vector listVector = new Vector(numItems);
    for (int i = 0; i < numItems; i++) {
      listVector.addElement(listPrefix + i);
    }
    JList list = new JList(listVector);
    list.setSelectedIndex(1); 
    list.addFocusListener(this);
    JScrollPane listScrollPane = new JScrollPane(list);

    listScrollPane.getVerticalScrollBar().setFocusable(false);
    listScrollPane.getHorizontalScrollBar().setFocusable(false);
    add(listScrollPane);

    setPreferredSize(new Dimension(450, 450));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  }
  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") );
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("FocusEventDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent newContentPane = new Main();
    newContentPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.pack();
    frame.setVisible(true);

  }
  public void focusGained(FocusEvent e) {
    displayMessage("Focus gained", e);
}

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

   
    
  








Related examples in the same category

1.Setting Focus Traversal Keys in a Component
2.Change the forward focus traversal keys for a component
3.JComponent.setFocusTraversalKeys(int arg0, Set arg1)
4.KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
5.Setting Focus Traversal Keys for the Entire Application
6.Changes the focus traversal keys for the entire application.
7.Change the backward focus traversal keys for the application
8.KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS
9.KeyboardFocusManager.getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)
10.Remove all forward focus traversal keys for the application
11.Remove all backward focus traversal keys for the application
12.Modify a component: press space bar or F2 to move focus to next focusable component.
13.Press shift space or shift F2 to move the focus to the previous focusable component.
14.Setting the Initial Focused Component in a Window
15.Finding the Next Focusable Component
16.Find the previous focusable component
17.Determining If a Focus Lost Is Temporary or Permanent
18.Determining the Opposite Component of a Focus Event
19.Validating a JTextField When Permanently Losing the Focus
20.Removing the Focus from the Application
21.Null is returned if none of the components in this application has the focus
22.Null is returned if none of the windows in this application has the focus
23.Listening to All Focus Changes Between Components and windows
24.Preventing a Component from Gaining the Focus