[英]Eclipse Wizard: how to add a checkboxlist?
我嘗試使用復選框列表創建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的包裝器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.