我在选择Table
行时遇到问题。总是当我点击CCombo
时,未选中该行。 (请参阅system.output为0或-1)。如何在SWT表中选择一行。我希望行SelectionListener
。谢谢你的回答。一个例子如下:
public class FrameVerwaltung {
private Display display;
private Shell shell;
private Table tableLoc;
private String[] colNames = { "Name", "ToDo",};
private static FrameVerwaltung frame;
private org.eclipse.swt.graphics.Color colorYellow;
public static void main (String[] args) {
frame = new FrameVerwaltung();
}
public FrameVerwaltung() {
display = new Display();
shell = new Shell(display);
FormLayout layout = new FormLayout();
shell.setLayout(layout);
colorYellow = display.getSystemColor(SWT.COLOR_YELLOW);
pSetTable();
shell.setText("");
shell.pack();
//shell.setSize(1500, 1000);
shell.open();
shell.setMaximized(true);
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
private void pSetTable() {
tableLoc = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
GridData gridData= new GridData();
tableLoc.setLinesVisible(true);
tableLoc.setHeaderVisible(true);
pSetTableColumn();
//*table.setToolTipText("Eine Tabelle");
psetTableRows();
}
private void pSetTableColumn() {
TableColumn[] cols = new TableColumn[colNames.length];
int[] order = new int[colNames.length];
for (int i = 0; i < colNames.length; i++) {
TableColumn tc = new TableColumn(tableLoc, SWT.BORDER);
tc.setText(colNames[i]);
switch (i){
case 0:
tc.setWidth(200);
break;
case 1:
tc.setWidth(50);
break;
}
tc.setResizable(true);
//tc.setMoveable(true);
cols[i] = tc;
order[i] = i;
}
}
private void psetTableRows() {
for (int k = 0; k < 7; k++) {
TableItem itemTmp = new TableItem(tableLoc, SWT.BORDER);
itemTmp.setText(0,String.valueOf(k));
pSetTableRowToDo(itemTmp);
}
}
private void pSetTableRowToDo(TableItem itemIn) {
TableEditor editor = new TableEditor(tableLoc);
CCombo comboboxAction = new CCombo(tableLoc, SWT.BORDER | SWT.CHECK | SWT.MULTI
| SWT.FULL_SELECTION);
comboboxAction.setText("=");
comboboxAction.add("=");
comboboxAction.add(">");
comboboxAction.add("<");
comboboxAction.add("?");
comboboxAction.add("x");
editor.grabHorizontal = true;
editor.setEditor(comboboxAction,itemIn,1);
editor = new TableEditor(tableLoc);
comboboxAction.addSelectionListener(new SelectionListener() {
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void widgetSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
for (int i=0;i<7;i++) {
CCombo ccoboTmp = (CCombo) arg0.getSource();
TableItem[] tableItem = tableLoc.getSelection();
System.out.println(tableItem.length);
System.out.println(tableLoc.getSelectionIndex());
}
}
});
}
}
答案 0 :(得分:1)
解决方案1:
在选择CCombo时选择表格行的一种方法是在创建每一行时将行索引传递给pSetTableRowToDo()
,然后在comboboxAction.addSelectionListener()
执行tableLoc.setSelection(rowToSelect)
,这样工作完美如下。
private void psetTableRows() {
for (int rowIndex = 0; rowIndex < 7; rowIndex++) {
TableItem itemTmp = new TableItem(tableLoc, SWT.BORDER);
itemTmp.setText(0, String.valueOf(rowIndex));
pSetTableRowToDo(itemTmp, rowIndex);
}
}
private void pSetTableRowToDo(TableItem itemIn, int rowIndex) {
TableEditor editor = new TableEditor(tableLoc);
CCombo comboboxAction = new CCombo(tableLoc, SWT.BORDER | SWT.CHECK | SWT.MULTI | SWT.FULL_SELECTION);
comboboxAction.setText("=");
comboboxAction.add("=");
comboboxAction.add(">");
comboboxAction.add("<");
comboboxAction.add("?");
comboboxAction.add("x");
editor.grabHorizontal = true;
editor.setEditor(comboboxAction, itemIn, 1);
editor = new TableEditor(tableLoc);
comboboxAction.addSelectionListener(new SelectionListener() {
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
}
@Override
public void widgetSelected(SelectionEvent arg0) {
tableLoc.setSelection(rowIndex);
tableLoc.forceFocus();
System.out.println("Selected Table Row Index: " +tableLoc.getSelectionIndex());
}
});
}
解决方案2:
另一种方法是获取tableItem的索引并将选择设置为该索引,其工作方式如下所示。您可能希望使用tableLoc.forceFocus();
,以便所选行将被聚焦。
private void pSetTableRowToDo(TableItem itemIn) {
TableEditor editor = new TableEditor(tableLoc);
CCombo comboboxAction = new CCombo(tableLoc, SWT.BORDER | SWT.CHECK | SWT.MULTI | SWT.FULL_SELECTION);
comboboxAction.setText("=");
comboboxAction.add("=");
comboboxAction.add(">");
comboboxAction.add("<");
comboboxAction.add("?");
comboboxAction.add("x");
editor.grabHorizontal = true;
editor.setEditor(comboboxAction, itemIn, 1);
editor = new TableEditor(tableLoc);
comboboxAction.addSelectionListener(new SelectionListener() {
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
}
@Override
public void widgetSelected(SelectionEvent arg0) {
int indexOf = tableLoc.indexOf(itemIn);
tableLoc.setSelection(indexOf);
tableLoc.forceFocus();
System.out.println("Selected Table Row Index: " + tableLoc.getSelectionIndex());
}
});
}
答案 1 :(得分:0)
在pSetTableRowToDo
方法中,您传递了TableItem
添加CCombo
的内容,因此在SelectionListener
中,您可以通过调用以下内容来利用此功能:
tableLoc.indexOf(itemIn);
(参见:Table.indexOf(TableItem)
)
请注意,您需要将itemIn
参数设为pSetTableRowToDo
final
,但这根本不是问题,实际上更可取。
使用该索引,您可以调用tableLoc.setSelection(int)
。