bonjour,
je cherche à résoudre un problème de prise de focus quand je navigue entre jpanels d'un cardLayout, peut être ne suis je pas dans le bon forum à vous de me dire s'il vous plaît.

requestFocusInWindow() doit être invoqué après que le composant soit "realized" mais avant que la frame soit affichée.

Ok mais qu'en est il dans le cas suivant :
- frame contient un panelTop en haut et un cardlayout en bas.
- panelTop contient tfTop
- cardlayout contient card1 et card2
- card1 contient cbBot1 et cbBot2
- card2 contient tfBot1 et tfBot2.

quand je fais "F1" et que card1 est actif je veux
- activer card2 et avec curseur sur tfBot2 directement, sans que focusGained() de tfBot1 n'ait été invoqué.
Et si le focus était sur un champ de card1, sans que focusGained() de tfTop n'ait été invoqué non plus.

En fait je veux que mon focus aille directement où je lui dis sans passer ailleurs.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class CardLayoutDemoFocus {
 
    String current = COMBO_PANEL;
    JPanel conteneur; //a panel that uses CardLayout
    final static String COMBO_PANEL = "Card with combos";
    final static String TEXTPANEL = "Card with JTextField";
    JComboBox cbBot1, cbBot2;
    JTextField tftop, tfBot1, tfBot2;
 
    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"});
        tftop = new JTextField("tfTop",20);
        tfBot1 = new JTextField("tfBot1", 20);
        tfBot2 = new JTextField("tfBot2", 20);
        tftop.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                System.out.println("perdu ...");
            }
        });
        topPane.add(tftop);
 
        //Create the "contenu 1".
        JPanel card1 = new JPanel();
        card1.add(cbBot1);
        card1.add(cbBot2);
 
        //Create the "contenu 2".
        JPanel card2 = new JPanel();
        card2.add(tfBot1);
        card2.add(tfBot2);
 
        //Create the panel that contains the "conteneur".
        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() {
        //Create and set up the window.
        JFrame frame = new JFrame("CardLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Create and set up the content pane.
        CardLayoutDemoFocus demo = new CardLayoutDemoFocus();
        demo.addComponentToPane(frame.getContentPane());
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
        } catch (IllegalAccessException ex) {
        } catch (InstantiationException ex) {
        } catch (ClassNotFoundException ex) {
        }
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);
 
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
 
    class ActionF1 extends AbstractAction {
 
        @Override
        public void actionPerformed(ActionEvent e) {
            if (first_f1) {
                first_f1 = false;
                CardLayout cl = (CardLayout) (conteneur.getLayout());
                Component toBeFocused = null;
                if (current.contains(COMBO_PANEL)) {
                    current = TEXTPANEL;
                    toBeFocused = tfBot1;
                } else if (current.contains(TEXTPANEL)) {
                    current = COMBO_PANEL;
                    toBeFocused = cbBot2;
                }
                toBeFocused.requestFocusInWindow();
                cl.show(conteneur, current);
            }
        }
    }
 
    class ActionF1Release extends AbstractAction {
 
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            first_f1 = true;
        }
    }
    boolean first_f1 = true;
}