Bonjour,

Je voudrais ajouter des scrollbars à une JList, voici mon code :

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
 
public class TestLayout2 extends JFrame {
 
    private JList list1;
 
    public TestLayout2() {
 
        this.getContentPane().setLayout(new GridBagLayout());
 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
 
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH; // components grow in both dimensions
 
 
        JPanel panelList1 = new JPanel();
        panelList1.setPreferredSize(new Dimension(panelWidth, panelHeight)); // largeur, hauteur
        panelList1.setBackground(new Color(255, 255, 255));
        panelList1.setBorder(new LineBorder(Color.blue));
        panelList1.setAlignmentX(Panel.LEFT_ALIGNMENT);
        final DefaultListModel model1 = new DefaultListModel();
        for (int i=0; i<3; i++) {model1.addElement("toto"+i);}
        model1.addElement("oooooooooooooyyyyyyy");
        list1 = new JList(model1);
        list1.setPreferredSize(new Dimension(listWidth, listHeight)); // largeur, hauteur
        list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        list1.setFont(new Font("Dialog", Font.BOLD, fontSize));
        list1.setBorder(new LineBorder(Color.blue));
 
        panelList1.add(list1);
 
        JScrollPane scrollpane = new JScrollPane(list1);
        scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);        
 
        panelList1.add(scrollpane);
 
...

mais les scrollbars ne s'affichent pas, pourquoi ?

Merci,

Nico