java - eclipse 向导 : how to add a checkboxlist?

标签 java eclipse wizard jcheckbox

我尝试创建一个带有复选框列表的 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/

相关文章:

java - 如何处理正则表达式中的多个括号?

java - 在eclipse中的方法中获取字段类型

java - Mockito when() 不工作

java - JAXB UnmarshalException - 包含 # 的文件名

linux - 用于 C/C++ 的 Eclipse IDE 中的远程调试

java - jface 向导中的 onclick 下一个按钮事件

Delphi OpenTools API - 编辑项目需要子句

jquery - 如何使用 jquery-validate 验证 Fuelux 向导中的每个步骤

java - 创建数据库时将数据添加到sqlite数据库

Eclipse:在服务器上运行选项对于 Maven 项目不可见