我尝试创建一个带有复选框列表的 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);
但我只看到一个空的白框。
怎么了?
最佳答案
在不同的帖子中有预期和建议的更简单的解决方案。
将标志 SWT.CHECK 添加到 TableViewer 会创建一个列表,其中包含来自 ArrayList 的复选框。
因此不再需要带有 CheckBoxList 的包装器。
关于java - eclipse 向导 : how to add a checkboxlist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14705432/