comment redéfinir focusNextComponent
bonjour, la question est dans le titre.
Comment redéfinir focusNextComponent() pour que je fasse une action sur le composant suivant AVANT qu'il n'ait le focus ?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class ActionFocusMover implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.focusNextComponent();
}
}
public class FocusSampleActionFocusMover {
public static void main(String args[]) {
JFrame frame = new JFrame("Focus Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener actionListener = new ActionFocusMover();
frame.setLayout(new GridLayout(3, 3));
for (int i = 1; i < 10; i++) {
JButton button = new JButton(Integer.toString(i));
button.addActionListener(actionListener);
if ((i % 2) != 0) {
button.setFocusable(false);
}
frame.add(button);
}
frame.setSize(300, 200);
frame.setVisible(true);
}
} |