Bonjour,

Je veux créer un panel qui me permette d'afficher les diffrentes options d'une appli.
J'ai donc un panel qui contient un Jtree et un PGAOptionPanel qui hérite de Panel.

Quand je selectionne un item dans le tree je veux afficher le bon PGAOptionPanel mais il ne s'affiche correctement que lorsque je change la taille de l'appli.

Voici le code qui fait ca:
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
 public void valueChanged(TreeSelectionEvent e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)
		        optionTree.getLastSelectedPathComponent();
 
        if (node == null)
            return;
 
        this.remove(optionPanel);
        optionPanel = (PGAOptionPanel) node.getUserObject();
        this.add(optionPanel, java.awt.BorderLayout.CENTER);
        // refesh the data
        optionPanel.refresh(); 
 
 
       this.repaint();
 
 
    }
Je fais un remove de l'ancien et un add du nouveau panel car juste changer la référence ne fonctionne pas du tout. En surchargeant la methode paint dans PGAOptionPanel je vois qu'elle n'est pa appelée. Seulement quand je resize la fenetre et la le panel s'affiche.

Quelqu'un sait il pourquoi ca ne marche pas et sinon comment faire pour changer dynamiquement un panel contenu dans un autre panel.

Merci

Code complet de la classe:
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
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
 
 
public class OptionPanel extends PGAPanel implements TreeSelectionListener{
 
    private static Properties prop;
    private static DefaultMutableTreeNode optionTreeRoot;
 
    static {
 
    	if (prop == null){
            try {
 
                InputStream inStream = OptionPanel.class.getClassLoader().getResourceAsStream("Option.properties");
                prop = new Properties();
                prop.load(inStream);
                inStream.close();
            } catch (IOException io) {
                Logger.log(PlugInManager.class,"Error while loading properties for OptionPanel" );
            }
 
        }
        PGAOptionPanel rootOptionPanel = new PGAOptionPanel();
        rootOptionPanel.setName(prop.getProperty("RootName"));
        optionTreeRoot = new DefaultMutableTreeNode(rootOptionPanel);
    }
 
 
    /** Creates new form OptionPanel */
    public OptionPanel() {
        initComponents();
        initTreeOption();
 
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        optionTree = new JTree(optionTreeRoot);
        optionTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        optionPanel = new PGAOptionPanel();
        jPanel1 = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
 
        setLayout(new java.awt.BorderLayout());
 
        DefaultTreeCellRenderer treeCellRenderer = new DefaultTreeCellRenderer(){
 
            public Component getTreeCellRendererComponent(JTree tree,
                Object value,
                boolean sel,
                boolean expanded,
                boolean leaf,
                int row,
                boolean hasFocus){
 
                super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
                Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
 
                if (userObject instanceof String)
                setText((String) userObject);
 
                else if (userObject instanceof PGAOptionPanel){
                    PGAOptionPanel pgaOptionPanel = (PGAOptionPanel) userObject;
                    setText(pgaOptionPanel.getName());
                }
                else
                setText("");
 
                return this;
 
            }
        };
        treeCellRenderer.setLeafIcon(null);
        optionTree.setCellRenderer(treeCellRenderer);
        jScrollPane1.setViewportView(optionTree);
 
        add(jScrollPane1, java.awt.BorderLayout.WEST);
 
        add(optionPanel, java.awt.BorderLayout.CENTER);
 
        okButton.setText(prop.getProperty("OKButtonLabel"));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
 
        cancelButton.setText(prop.getProperty("cancelButtonLabel"));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
 
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(195, Short.MAX_VALUE)
                .addComponent(okButton)
                .addGap(44, 44, 44)
                .addComponent(cancelButton)
                .addGap(23, 23, 23))
        );
 
        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});
 
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap(22, Short.MAX_VALUE))
        );
 
        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cancelButton, okButton});
 
        add(jPanel1, java.awt.BorderLayout.SOUTH);
 
    }// </editor-fold>                        
 
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        fireClosePanelEvent();
    }                                            
 
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (optionPanel != null){
            optionPanel.save();
        }
    }                                        
 
 
    // Variables declaration - do not modify                     
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    private com.sgme.pganacrim.ui.option.PGAOptionPanel optionPanel;
    private javax.swing.JTree optionTree;
    // End of variables declaration                   
 
 
 
 
    public static DefaultMutableTreeNode getOptionTreeRoot(){
        return optionTreeRoot;
    }
 
 
    public void initTreeOption(){
        optionTree.addTreeSelectionListener(this);
    }
 
    public void valueChanged(TreeSelectionEvent e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)
		        optionTree.getLastSelectedPathComponent();
 
        if (node == null)
            return;
 
        this.remove(optionPanel);
        optionPanel = (PGAOptionPanel) node.getUserObject();
        this.add(optionPanel, java.awt.BorderLayout.CENTER);
        optionPanel.refresh();
 
 
       this.repaint();
 
 
    }
 
}