bonsoir

j'essaie de définir une JList munie de JCheckboxes : une jlist où dans chaque ligne il y a une case à cocher.

ça ne marche pas : la JList ne s' affiche pas...

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
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
public class Fen1 {
 
    private JFrame frame;
 
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Fen1 window = new Fen1();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
 
    /**
     * Create the application.
     */
    public Fen1() {
        initialize();
    }
 
    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 345, 220);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
 
        JButton btnCoch = new JButton("Coch\u00E9?");
        btnCoch.setBounds(10, 149, 89, 23);
        frame.getContentPane().add(btnCoch);
 
        final JLabel lblIci = new JLabel("ICI");
        lblIci.setBounds(109, 153, 120, 14);
        frame.getContentPane().add(lblIci);
 
        JList list = new JList();
        list.setBounds(10, 11, 310, 127);
 
 
         final CheckListItem apple=new CheckListItem("apple");
         list = new JList(new CheckListItem[] {
                apple,
                new CheckListItem("orange"),
                new CheckListItem("mango"),
                new CheckListItem("paw paw"),
                new CheckListItem("banana")});
 
          // Use a CheckListRenderer (see below)
          // to renderer list cells
 
          list.setCellRenderer(new CheckListRenderer());
          list.setSelectionMode(
             ListSelectionModel.SINGLE_SELECTION);
 
          // Add a mouse listener to handle changing selection
 
          list.addMouseListener(new MouseAdapter()
          {
             public void mouseClicked(MouseEvent event)
             {
                JList list = (JList) event.getSource();
 
                // Get index of item clicked
 
                int index = list.locationToIndex(event.getPoint());
                CheckListItem item = (CheckListItem)
                   list.getModel().getElementAt(index);
 
                // Toggle selected state
 
                item.setSelected(! item.isSelected());
 
                // Repaint cell
 
                list.repaint(list.getCellBounds(index, index));
             }
          }); 
 
          btnCoch.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
 
                    if (apple.isSelected()){
                        lblIci.setText("coché");
 
                    }else{
                        lblIci.setText("décoché");
                    }
 
 
 
 
                }
            });
 
          frame.getContentPane().add(list);
 
    }
 
}
voici les classes auxiliaires:

checkListItem
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
class CheckListItem
{
   private String  label;
   private boolean isSelected = false;
 
   public CheckListItem(String label)
   {
      this.label = label;
   }
 
   public boolean isSelected()
   {
      return isSelected;
   }
 
   public void setSelected(boolean isSelected)
   {
      this.isSelected = isSelected;
   }
 
   public String toString()
   {
      return label;
   }
}

et CheckListCellRenderer

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
class CheckListRenderer extends JCheckBox
implements ListCellRenderer
{
/**
     * 
     */
    private static final long serialVersionUID = 1L;
 
public Component getListCellRendererComponent(
      JList list, Object value, int index,
      boolean isSelected, boolean hasFocus)
{
   setEnabled(list.isEnabled());
   setSelected(((CheckListItem)value).isSelected());
   setFont(list.getFont());
   setBackground(list.getBackground());
   setForeground(list.getForeground());
   setText(value.toString());
   return this;
}
}
pouvez-vous m'aider?