Bonjour à tous,
Voici ci-dessous un petit programme self-compilable qui affiche une fenêtre avec un bouton.
Lorsque je clique sur mon bouton, mon programme ajoute de manière dynamique des JLabel cependant ces JLabel ne s'affichent pas malgré les appels à invalidate() ou repaint()... Le souci, c'est qu'en bougeant le splitpane, mes labels s'affichent !
Je ne comprend pas pourquoi cela ne fonctionne pas...
Ci-dessous mon code:
Une idée de pourquoi ça ne fonctionne pas ?
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 package testjsplitpane; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.border.TitledBorder; public class TestJSplitPane { /** * @param args the command line arguments */ public static void main(String[] args) { new MainFrame().setVisible(true); } public static class MainFrame extends JFrame { private final static Dimension SIZE = new Dimension(800, 600); private JPanel rightPanel; private JPanel leftPanel; private JSplitPane splitPane; private static int number = 0; public MainFrame() throws HeadlessException { super(); initMainFrame(); initLeftPanel(); initLayout(); refresh(); } private void initMainFrame() { setMinimumSize(SIZE); setMaximumSize(SIZE); setPreferredSize(SIZE); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); } private void initLayout() { getContentPane().add(getSplitPane()); } private void initLeftPanel() { JButton button = new JButton("Add a label"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Adding a component..."); addLabel(new JLabel("Label #" + number)); number++; refresh(); } }); getLeftPanel().add(button, BorderLayout.NORTH); } public JPanel getLeftPanel() { if (leftPanel == null) { leftPanel = new JPanel(); TitledBorder leftBorder = BorderFactory.createTitledBorder("Left panel"); leftPanel.setBorder(leftBorder); leftPanel.setLayout(new BorderLayout()); } return leftPanel; } public JPanel getRightPanel() { if (rightPanel == null) { rightPanel = new JPanel(); TitledBorder rightBorder = BorderFactory.createTitledBorder("Right panel"); rightPanel.setBorder(rightBorder); rightPanel.setLayout(new GridBagLayout()); } return rightPanel; } private void addLabel(JComponent labelToAdd) { GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1; gbc.weighty = 0; gbc.gridx = 0; gbc.gridy = GridBagConstraints.RELATIVE; gbc.gridheight = GridBagConstraints.RELATIVE; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.fill = GridBagConstraints.HORIZONTAL; getRightPanel().add(labelToAdd, gbc); refresh(); } public JSplitPane getSplitPane() { if (splitPane == null) { splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); splitPane.setLeftComponent(getLeftPanel()); splitPane.setRightComponent(getRightPanel()); splitPane.setDividerLocation(150); splitPane.setOneTouchExpandable(false); } return splitPane; } public void refresh() { getRightPanel().invalidate(); invalidate(); repaint(); } } }
Merci d'avance !!!







Répondre avec citation



La plupart des réponses à vos questions sont déjà dans les 
Partager