gpt4 book ai didi

java - 如何将选定行的值从表查看器传递到 SWT 中的文本框

转载 作者:行者123 更新时间:2023-12-01 13:18:10 25 4
gpt4 key购买 nike

我有一个有 10 行的表格,我想要做的是:当我单击“修改”按钮时,我想将选定的表格行值传递到相应的文本框。

我尝试了一些代码,但不能完全满足我的需求,

MainTable.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
String string = "";
TableItem[] selection = MainTable.getSelection();
for (int i = 0; i < selection.length; i++)
string += selection[i] + " ";
final String Appname=string.substring(11, string.length()-2);
System.out.println(Appname);
}
});

上面的代码正在控制台中打印所选行的值,我想将这些值设置到文本框中。

我该怎么做?

最佳答案

这里是一些示例代码:

private static int        columns = 3;
private static List<Text> texts = new ArrayList<>();

public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(columns, false));

Table table = new Table(shell, SWT.FULL_SELECTION);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, columns, 1));
table.setHeaderVisible(true);

/* Create columns */
for (int col = 0; col < columns; col++)
{
new TableColumn(table, SWT.NONE).setText("Col " + col);
}

/* Create cells */
for (int row = 0; row < 10; row++)
{
TableItem item = new TableItem(table, SWT.NONE);

for (int col = 0; col < table.getColumnCount(); col++)
{
item.setText(col, "Cell " + row + " " + col);
}
}

/* Pack columns */
for (int col = 0; col < table.getColumnCount(); col++)
{
table.getColumn(col).pack();
}

/* Create the Text fields */
for (int col = 0; col < columns; col++)
{
Text text = new Text(shell, SWT.BORDER);
texts.add(text);
}

/* Listen for selection */
table.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event e)
{
Table table = (Table) e.widget;
TableItem item = table.getItem(table.getSelectionIndex());

/* Fill the texts */
for (int col = 0; col < table.getColumnCount(); col++)
{
texts.get(col).setText(item.getText(col));
}
}
});

shell.pack();
shell.open();

while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

看起来像这样:

enter image description here

关于java - 如何将选定行的值从表查看器传递到 SWT 中的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22297732/

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