我是SWT的初学者,有人请帮忙, 我的问题是: 我有一个表,有10列, 第一列有复选框,列的其余部分从数据库填充, 当我选中复选框时,我希望选择一个特定的行,
我正在粘贴下面的表格代码,
public static void main(String arg[])
{
// shell and display declared here
mainTable = new TableViewer(content, SWT.H_SCROLL| SWT.V_SCROLL | SWT.BORDER |SWT.DM_FILL_BACKGROUND|SWT.FULL_SELECTION);
mainTable .getTable().setHeaderVisible(true);
mainTable .getTable().setLinesVisible(true);
mainTable .setContentProvider(new ArrayContentProvider());
// COLUM FOR PLACING THE CHECK BOXES
TableColumn columncOL=new TableColumn(mainTable .getTable(),SWT.NONE);
columncOL.setText("");
columncOL.setWidth(40);
TableViewerColumn columnaC=new TableViewerColumn(mainTable ,columncOL);
columnaC.setLabelProvider(new ColumnLabelProvider(){
Map<Object, Button> buttons = new HashMap<Object, Button>();
@Override
public void update(ViewerCell cell) {
TableItem item = (TableItem) cell.getItem();
Button button;
if(buttons.containsKey(cell.getElement()))
{
button = buttons.get(cell.getElement());
}
else
{
button = new Button((Composite) cell.getViewerRow().getControl(),SWT.CHECK);
String s = "Item 1";
button.setData(s, "Some other info or object here");
s = "Item 2";
button.setData(s, "This is item two");
//buttons.put(cell.getElement(), button);
}
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button , item, cell.getColumnIndex());
editor.layout();
}
});
// CREATING 1st COLUMNS
TableColumn column1Head=new TableColumn(mainTable.getTable(),SWT.NONE);
column1Head.setText("col1");
column1Head.setWidth(200);
// setting column input
TableViewerColumn column1=new TableViewerColumn(mainTable,column1Head);
column1.setLabelProvider(new ColumnLabelProvider()
{
public String getText(Object Element)
{
Student ap=(Student )Element;
return ap.col1();
}
});
}
像这样创建剩余的8列
我想在选中perticluar复选框时选择整行。
任何一个pleeeease尽早帮助我....
答案 0 :(得分:2)
使用CheckboxTableViewer
显示带有复选框的表格:
mainTable = CheckboxTableViewer.newCheckList(content, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
要选中勾选复选框后的行,您需要使用addCheckStateListener
选择选中的行,例如:
mainTable.addCheckStateListener(new ICheckStateListener()
{
@Override
public void checkStateChanged(CheckStateChangedEvent event)
{
if (event.getChecked())
mainTable.setSelection(new StructuredSelection(event.getObject()));
}
});
答案 1 :(得分:1)
简单方法:表格的第一列在每行中都有复选框 选择与否
CheckboxTableViewer tableViewer = CheckboxTableViewer.newCheckList(parent, SWT.BORDER | SWT.FULL_SELECTION);
Table table = tableViewer.getTable();
table.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event e) {
Table table = (Table) e.widget;
TableItem item = table.getItem(table.getSelectionIndex());
for (int col = 0; col < table.getColumnCount(); col++)
{
//Table_Column Checked or Not
if(item.getChecked())
//return true
//all ready checked(true) to change unchecked(false)
item.setChecked(false);
else
//unchecked(false) to checked(true) each row
item.setChecked(true);
}
}
}
这对你有用....谢谢