Java by API/java.awt/KeyboardFocusManager
Содержание
- 1 KeyboardFocusManager: addPropertyChangeListener(PropertyChangeListener listener)
- 2 KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS
- 3 KeyboardFocusManager: clearGlobalFocusOwner()
- 4 KeyboardFocusManager: focusNextComponent()
- 5 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
- 6 KeyboardFocusManager: getFocusedWindow()
- 7 KeyboardFocusManager: getFocusOwner()
- 8 KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
KeyboardFocusManager: addPropertyChangeListener(PropertyChangeListener listener)
import java.awt.ruponent;
import java.awt.KeyboardFocusManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class Main {
public static void main(String[] argv) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(
new FocusChangeListener());
}
}
class FocusChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
Component oldComp = (Component) evt.getOldValue();
Component newComp = (Component) evt.getNewValue();
if ("focusOwner".equals(evt.getPropertyName())) {
if (oldComp == null) {
System.out.println(newComp.getName());
} else {
System.out.println(oldComp.getName());
}
} else if ("focusedWindow".equals(evt.getPropertyName())) {
if (oldComp == null) {
System.out.println(newComp.getName());
} else {
System.out.println(oldComp.getName());
}
}
}
}
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS
import java.awt.AWTKeyStroke;
import java.awt.KeyboardFocusManager;
import java.util.HashSet;
import java.util.Set;
import javax.swing.KeyStroke;
public class Main {
public static void main(String[] argv) throws Exception {
Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(KeyboardFocusManager
.getCurrentKeyboardFocusManager().getDefaultFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
set.add(KeyStroke.getKeyStroke("F3"));
KeyboardFocusManager.getCurrentKeyboardFocusManager().setDefaultFocusTraversalKeys(
KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set);
}
}
KeyboardFocusManager: clearGlobalFocusOwner()
import java.awt.KeyboardFocusManager;
public class Main {
public static void main(String[] argv) throws Exception {
KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
}
}
KeyboardFocusManager: focusNextComponent()
import java.awt.ruponent;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Focus Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener actionListener = new ActionFocusMover();
MouseListener mouseListener = new MouseEnterFocusMover();
frame.setLayout(new GridLayout(3, 3));
for (int i = 1; i < 10; i++) {
JButton button = new JButton(Integer.toString(i));
button.addActionListener(actionListener);
button.addMouseListener(mouseListener);
if ((i % 2) != 0) {
button.setFocusable(false);
}
frame.add(button);
}
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class ActionFocusMover implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
KeyboardFocusManager manager = KeyboardFocusManager
.getCurrentKeyboardFocusManager();
manager.focusNextComponent();
}
}
class MouseEnterFocusMover extends MouseAdapter {
public void mouseEntered(MouseEvent mouseEvent) {
Component component = mouseEvent.getComponent();
if (!component.hasFocus()) {
component.requestFocusInWindow();
}
}
}
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS
import java.awt.AWTKeyStroke;
import java.awt.KeyboardFocusManager;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.KeyStroke;
public class Main {
public static void main(String[] argv) throws Exception {
JButton component = new JButton("a");
Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(component
.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
set.add(KeyStroke.getKeyStroke("F2"));
component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);
}
}
KeyboardFocusManager: getFocusedWindow()
import java.awt.KeyboardFocusManager;
import java.awt.Window;
public class Main {
public static void main() {
Window windowFocusOwner =
KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
}
}
KeyboardFocusManager: getFocusOwner()
import java.awt.ruponent;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.FocusTraversalPolicy;
import java.awt.KeyboardFocusManager;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String[] argv) {
JFrame frame = new JFrame();
JButton component1 = new JButton("1");
JButton component2 = new JButton("2");
JButton component3 = new JButton("3");
frame.setLayout(new FlowLayout());
frame.add(component1);
frame.add(component2);
frame.add(component3);
frame.pack();
frame.setVisible(true);
System.out.println(findPrevFocus().getName());
}
public static Component findPrevFocus() {
Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
Container root = c.getFocusCycleRootAncestor();
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
Component prevFocus = policy.getComponentBefore(root, c);
if (prevFocus == null) {
prevFocus = policy.getDefaultComponent(root);
}
return prevFocus;
}
}
KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
import java.awt.AWTKeyStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame {
public MainClass() {
super("Focus Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
MyPanel mypanel = new MyPanel();
JButton button1 = new JButton("One");
JButton button2 = new JButton("Two");
JButton button3 = new JButton("Three");
JButton button4 = new JButton("Four");
JButton button5 = new MyButton("Five*");
JButton button6 = new MyButton("Six*");
JButton button7 = new JButton("Seven");
mypanel.add(button2);
mypanel.add(button3);
JInternalFrame frame1 = new JInternalFrame("Internal Frame 1", true, true, true, true);
frame1.setBackground(Color.lightGray);
frame1.getContentPane().setLayout(new GridLayout(2, 3));
frame1.setSize(300, 200);
frame1.getContentPane().add(button1);
frame1.getContentPane().add(mypanel);
frame1.getContentPane().add(button4);
frame1.getContentPane().add(button5);
frame1.getContentPane().add(button6);
frame1.getContentPane().add(button7);
JDesktopPane desktop = new JDesktopPane();
desktop.add(frame1, new Integer(1));
desktop.setOpaque(true);
// Now set up the user interface window.
Container contentPane = getContentPane();
contentPane.add(desktop, BorderLayout.CENTER);
setSize(new Dimension(400, 300));
frame1.setVisible(true);
setVisible(true);
}
public static void main(String[] args) {
new MainClass();
}
}
class MyButton extends JButton {
public MyButton(String s) {
super(s);
}
public boolean isFocusable() {
return false;
}
}
class MyPanel extends JPanel {
public MyPanel() {
super(true);
java.util.Set upKeys = new java.util.HashSet(1);
upKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_UP, 0));
setFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, upKeys);
}
public boolean isFocusCycleRoot() {
return true;
}
}