gpt4 book ai didi

java - 从 TableItem 中检索小部件

转载 作者:行者123 更新时间:2023-11-30 09:23:13 26 4
gpt4 key购买 nike

我构建了一个表 (org.eclipse.swt.widgets.Table) 并使用 TableEditor 在每个 TableItem(行)上添加了 2 个复选框。

TableItem tableItem = new TableItem(tableClientes, SWT.NONE);
TableEditor editor = new TableEditor(tableClientes);

Button checkEnviar = new Button(tableClientes, SWT.CHECK);
checkEnviar.pack();

editor.minimumWidth = checkEnviar.getSize().x + 5;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(checkEnviar, tableItem, 0);

editor = new TableEditor(tableClientes);

Button checkMarcar = new Button(tableClientes, SWT.CHECK);
checkMarcar.pack();

editor.minimumWidth = checkMarcar.getSize().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(checkMarcar, tableItem, 1);

现在我需要读取每一行上每个复选框的状态,但我不知道该怎么做。

我尝试以这种方式检索 TableItem:

tableClientes.getItem(x)

但在它的内容中,我无法检索复选框状态或对其进行操作。任何想法如何做到这一点?

最佳答案

一个简单的解决方案是使用 Widget 类的 setData() 方法。例如,请看下面的代码(特别是最后一个按钮的选择监听器,但是测试一个有效的小部件实例):

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class TableItemTest {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
shell.setLayoutData(gridData);

final Table table = new Table(shell, SWT.BORDER);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setBounds(new org.eclipse.swt.graphics.Rectangle(47,67,190,70));

TableColumn tableColumn = new TableColumn(table, SWT.NONE);
tableColumn.setWidth(180);
tableColumn.setText("Check Column");

for(int i=0; i<10; i++){
Button checkButton = new Button(table, SWT.CHECK);
checkButton.pack();

TableItem tableItem=new TableItem(table,SWT.NONE);
TableEditor editor = new TableEditor (table);

editor.minimumWidth = checkButton.getSize ().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(checkButton, tableItem, 0);
tableItem.setData("CHECK_BOX", checkButton); //Point to notice
}

Button checkState = new Button(shell, SWT.PUSH);
checkState.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
checkState.setText("Check State");
checkState.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
TableItem[] items = table.getItems();
for (int i = 0; i < items.length; i++) {
Object data = items[i].getData("CHECK_BOX");
if(data != null){
Button checkButton = (Button)data;
if(!checkButton.isDisposed())
System.out.println("For row " + (i+1) + " check status is " + checkButton.getSelection());
}
}
}
});

shell.setSize(220, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

关于java - 从 TableItem 中检索小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16180016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com