简体   繁体   English

Eclipse向导:如何添加复选框列表?

[英]Eclipse Wizard: how to add a checkboxlist?

I try to create a Eclipse wizard page with an checkboxlist. 我尝试使用复选框列表创建Eclipse向导页面。

First I created a JList with JCheckBoxes : 首先,我使用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;
    }

}
}

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? 怎么了?

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. 将标志SWT.CHECK添加到TableViewer会创建一个列表,其中ArrayList中的复选框被选中。

So the wrapper with a CheckBoxList is not needed any longer. 因此,不再需要带有CheckBoxList的包装器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM