我创建了一个包含两列的tableViewer,我想让其中一列成为复选框。为此,我创建了一个CheckBoxCellEditor,但我不知道为什么它不起作用。
名为tableName的列显示其值为OK。
列规范如下。
String[] COLUMN_HEADINGS = {"Select", "Table name"};
TableColumnLayout tableLayout = new TableColumnLayout();
parent.setLayout(tableLayout);
//Set what would display each column
TableViewerColumn selectCheckBox = createTableViewerColumn(COLUMN_HEADINGS[0], 0);
selectCheckBox.getColumn().setResizable(false);
selectCheckBox.setLabelProvider(new ColumnLabelProvider(){
@Override
public String getText(Object element) {
return null;
}
});
//Set what would display each column
TableViewerColumn tableName = createTableViewerColumn(COLUMN_HEADINGS[1], 1);
tableName.getColumn().setResizable(false);
tableName.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof TableMetaData && element != null)
return ((TableMetaData)element).getName();
return super.getText(element);
}
});
//Set the dimensions of each column
tableLayout.setColumnData(selectCheckBox.getColumn(), new ColumnWeightData(10));
tableLayout.setColumnData(tableName.getColumn(), new ColumnWeightData(90));
//Set column type (checkbox)
selectCheckBox.setEditingSupport(new ResourcesConfigCheckEditingSupport(this));
编辑支持如下:
public class ResourcesConfigCheckEditingSupport extends EditingSupport{
private CheckboxCellEditor cellEditor;
public ResourcesConfigCheckEditingSupport(ColumnViewer viewer) {
super(viewer);
// TODO Auto-generated constructor stub
cellEditor = new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
}
@Override
protected CellEditor getCellEditor(Object element) {
// TODO Auto-generated method stub
return cellEditor;
}
@Override
protected boolean canEdit(Object element) {
// TODO Auto-generated method stub
return false;
}
@Override
protected Object getValue(Object element) {
// TODO Auto-generated method stub
return ((TableMetaData) element).getIsSelected();
}
@Override
protected void setValue(Object element, Object value) {
// TODO Auto-generated method stub
((TableMetaData) element).setIsSelected(Boolean.valueOf((boolean) value));
getViewer().update(element, null);
}
}
TableMetaData对象决定是否要选中Checkbox。如何修复我的代码才能使其正常工作?
谢谢。
答案 0 :(得分:0)
Vogella有一个完整的例子,你可以在这里找到:http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html
在第4节中,使用CheckboxCellEditor
。它可能会对你有帮助。
答案 1 :(得分:0)
为了使其成功,我使用Vogella的解决方案使用图像。问题是图像不能在表格的单元格中居中,它们出现在它们的左侧。我曾尝试使用“X”和“ - ”,但我不知道第一列会发生什么,它既不会使文本居中。所以为了解决这个问题,我改变了列的顺序,并且我使用了unicode字符。
解决方案代码如下编辑支持:
public class ResourcesConfigCheckEditingSupport extends EditingSupport{
private CheckboxCellEditor cellEditor;
public ResourcesConfigCheckEditingSupport(ColumnViewer viewer) {
super(viewer);
// TODO Auto-generated constructor stub
//cellEditor = new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
cellEditor = new CheckboxCellEditor(((TableViewer)viewer).getTable());
}
@Override
protected CellEditor getCellEditor(Object element) {
// TODO Auto-generated method stub
return cellEditor;
}
@Override
protected boolean canEdit(Object element) {
// TODO Auto-generated method stub
return true;
}
@Override
protected Object getValue(Object element) {
// TODO Auto-generated method stub
return ((TableMetaData) element).getIsSelected();
}
@Override
protected void setValue(Object element, Object value) {
// TODO Auto-generated method stub
((TableMetaData) element).setIsSelected(Boolean.valueOf((boolean) value));
getViewer().update(element, null);
}
}
以下创建列:
public class ResourcesConfigViewer extends TableViewer{
public ResourcesConfigViewer(Composite parent,int style)
{
super(parent, style);
//Get the table and set the headers visible and the lines
Table table = getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
//Create the columns of the tableviewer
createColumns(parent);
//Set the tableviewer contentProvider
setContentProvider(new ResourcesConfigTableContentProvider());
}
/*With this function we create the tableviewer columns and how they will (TextCell) and what they would display*/
private void createColumns(Composite parent)
{
//Headers of the table
String[] COLUMN_HEADINGS = {"Table name", "Select"};
TableColumnLayout tableLayout = new TableColumnLayout();
parent.setLayout(tableLayout);
//Set what would display each column
TableViewerColumn tableName = createTableViewerColumn(COLUMN_HEADINGS[0], 0);
tableName.getColumn().setResizable(false);
tableName.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof TableMetaData && element != null)
return ((TableMetaData)element).getName();
return super.getText(element);
}
});
//Set what would display each column
TableViewerColumn selectCheckBox = createTableViewerColumn(COLUMN_HEADINGS[1], 1);
selectCheckBox.getColumn().setResizable(false);
selectCheckBox.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof TableMetaData && element != null){
if(((TableMetaData)element).getIsSelected()){
return Character.toString((char)0x2611);
}else{
return Character.toString((char)0x2610);
}
}
return super.getText(element);
//return null;
}
@Override
public Image getImage(Object element) {
/*if(element instanceof TableMetaData && element != null){
if(((TableMetaData)element).getIsSelected()){
return checked;
}else{
return unChecked;
}
}
return super.getImage(element);*/
return null;
}
});
//Set the dimensions of each column
tableLayout.setColumnData(selectCheckBox.getColumn(), new ColumnWeightData(10));
tableLayout.setColumnData(tableName.getColumn(), new ColumnWeightData(90));
//Set column type (checkbox)
selectCheckBox.setEditingSupport(new ResourcesConfigCheckEditingSupport(this));
}
//Creates the column
private TableViewerColumn createTableViewerColumn(String header, int idx)
{
//To put checkbox centered in cell.
int infoLocation = SWT.LEFT;
if(idx == 1){
infoLocation = SWT.CENTER;
}
TableViewerColumn column = new TableViewerColumn(this, infoLocation, idx);
column.getColumn().setText(header);
column.getColumn().setResizable(true);
column.getColumn().setMoveable(true);
return column;
}
}
答案 2 :(得分:0)
TableViewer viewer = new TableViewer(container, SWT.CHECK | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
但我必须手动更改输入
viewer.getTable().addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
TableItem item = (TableItem) event.item;
Delay aDelay = (Delay) event.item.getData();
}
});