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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FocusTraversalPolicy;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class CardLayoutDemoFocus {
static ArrayList<Component> lstCbPnl = new ArrayList<Component>();
static ArrayList<Component> lstTfPnl = new ArrayList<Component>();
String current = COMBO_PANEL;
JPanel conteneur;
final static String COMBO_PANEL = "Card with combos";
final static String TEXTPANEL = "Card with JTextFields";
JComboBox cbBot1, cbBot2;
JTextField tftop, tfBot1, tfBot2;
static JFrame frame;
public void addComponentToPane(Container pane) {
String sActionF1 = "f1";
String sActionReleaseF1 = "rf1";
((JPanel) pane).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, false), sActionF1);
((JPanel) pane).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, true), sActionReleaseF1);
((JPanel) pane).getActionMap().put(sActionF1, new ActionF1());
((JPanel) pane).getActionMap().put(sActionReleaseF1, new ActionF1Release());
JPanel topPane = new JPanel(); //use FlowLayout
cbBot1 = new JComboBox(new String[]{"cb1"});
cbBot2 = new JComboBox(new String[]{"cb2"});
tfBot1 = new JTextField("tfBot1", 9);
tfBot2 = new JTextField("tfBot2", 9);
// https://tips4java.wordpress.com/2010/03/14/dialog-focus/
tfBot2.addAncestorListener(new RequestFocusListener(false));
cbBot2.addAncestorListener(new RequestFocusListener(false));
tftop = new JTextField("tfTop", 19);
tftop.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
System.out.println("perdu ...");
}
});
topPane.add(tftop);
//Create "contenu 1".
JPanel card1 = new JPanel();
card1.add(cbBot1);
card1.add(cbBot2);
lstCbPnl.add(cbBot2);
lstCbPnl.add(cbBot1);
lstCbPnl.add(tftop);
//Create "contenu 2".
JPanel card2 = new JPanel();
card2.add(tfBot1);
card2.add(tfBot2);
lstTfPnl.add(tfBot2);
lstTfPnl.add(tfBot1);
lstTfPnl.add(tftop);
//Create the container that contains the "cardLayout".
conteneur = new JPanel(new CardLayout());
conteneur.add(card1, COMBO_PANEL);
conteneur.add(card2, TEXTPANEL);
pane.add(topPane, BorderLayout.PAGE_START);
pane.add(conteneur, BorderLayout.CENTER);
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event dispatch thread.
*/
private static void createAndShowGUI() {
frame = new JFrame("CardLayoutDemo");
frame.setPreferredSize(new Dimension(260, 130));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CardLayoutDemoFocus demo = new CardLayoutDemoFocus();
demo.addComponentToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setFocusTraversalPolicy(new MyOwnFocusTraversalPolicy(lstCbPnl));
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
boolean first_f1 = true;
class ActionF1 extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
if (first_f1) {
first_f1 = false;
CardLayout cl = (CardLayout) (conteneur.getLayout());
ArrayList lFocus = null;
if (current.contains(COMBO_PANEL)) {
current = TEXTPANEL;
lFocus = lstTfPnl;
} else if (current.contains(TEXTPANEL)) {
current = COMBO_PANEL;
lFocus = lstCbPnl;
}
frame.setFocusTraversalPolicy(
new MyOwnFocusTraversalPolicy(lFocus));
cl.show(conteneur, current);
}
}
}
class ActionF1Release extends AbstractAction {
@Override
public void actionPerformed(ActionEvent actionEvent) {
first_f1 = true;
}
}
public static class MyOwnFocusTraversalPolicy
extends FocusTraversalPolicy {
ArrayList<Component> order;
public MyOwnFocusTraversalPolicy(ArrayList<Component> order) {
this.order = new ArrayList<Component>(order.size());
this.order.addAll(order);
}
@Override
public Component getComponentAfter(Container focusCycleRoot,
Component aComponent) {
int idx = (order.indexOf(aComponent) + 1) % order.size();
return order.get(idx);
}
@Override
public Component getComponentBefore(Container focusCycleRoot,
Component aComponent) {
int idx = order.indexOf(aComponent) - 1;
if (idx < 0) {
idx = order.size() - 1;
}
return order.get(idx);
}
@Override
public Component getDefaultComponent(Container focusCycleRoot) {
return order.get(0);
}
@Override
public Component getLastComponent(Container focusCycleRoot) {
return order.get(order.size() - 1);
}
@Override
public Component getFirstComponent(Container focusCycleRoot) {
return order.get(0);
}
}
} |
Partager