Search code examples
javaeclipsewizardjcheckbox

Eclipse Wizard: how to add a checkboxlist?


I try to create a Eclipse wizard page with an checkboxlist.

First I created a JList with JCheckBoxes :

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class CheckBoxList extends JList<JCheckBox> {

private static final long serialVersionUID = 1L;
protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);

public CheckBoxList() {
    setCellRenderer(new CellRenderer());

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            int index = locationToIndex(e.getPoint());

            if (index != -1) {
                JCheckBox checkbox = (JCheckBox) getModel().getElementAt(
                        index);
                checkbox.setSelected(!checkbox.isSelected());
                repaint();
            }
        }
    });

    setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}


protected class CellRenderer implements ListCellRenderer<JCheckBox> {
    public Component getListCellRendererComponent(
            JList<? extends JCheckBox> list, JCheckBox value, int index,
            boolean isSelected, boolean cellHasFocus) {
        JCheckBox checkbox = value;
        checkbox.setBackground(isSelected ? getSelectionBackground()
                : getBackground());
        checkbox.setForeground(isSelected ? getSelectionForeground()
                : getForeground());
        checkbox.setEnabled(isEnabled());
        checkbox.setFont(getFont());
        checkbox.setFocusPainted(false);
        checkbox.setBorderPainted(true);
        checkbox.setBorder(isSelected ? UIManager
                .getBorder("List.focusCellHighlightBorder") : noFocusBorder);
        return checkbox;
    }

}
}

And then I try to view it on the wizard page:

SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL | SWT.NULL);  

TableViewer tableViewer = new TableViewer(sashForm, SWT.V_SCROLL | SWT.WRAP);
tableViewer.setContentProvider(ArrayContentProvider.getInstance()); 
Table table = tableViewer.getTable();
table.setLinesVisible(true);


CheckBoxList cbl = new CheckBoxList();
cbl.add(new JCheckBox("Box1"));
cbl.add(new JCheckBox("Box2"));

tableViewer.setInput(cbl);

but all I see is an empty white box.

What is wrong?


Solution

  • There is much easier solution as expected and suggested in different postings.

    Adding the flag SWT.CHECK to TableViewer creates a List with checkboxed Items out of an ArrayList.

    So the wrapper with a CheckBoxList is not needed any longer.