我尝试创建一个带有复选框列表的 Eclipse 向导页面。
首先,我使用 JCheckBoxes 创建了一个 JList :
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;
}
}
}
然后我尝试在向导页面上查看它:
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);
但我看到的只是一个空的白盒子。
怎么了?