【发布时间】:2016-03-11 00:13:06
【问题描述】:
澄清一下,我只是在使用 SWT,而不是 JFace(尝试一次学习一个 GUI 库,尽管这可能不明智)。 Adding buttons to a table (java swt) 给出了一个 TreeEditor 的示例,我目前没有使用它。
我遇到的问题是,在我的一生中,我似乎无法在第 0 行第 0 列中显示按钮:
我在调试过程中注意到的一件事是检查按钮向下移动了一个;第 1 行中的第一个可见按钮实际上对应于第 0 项。
下面的代码是用 Scala 编写的,尽管它足够基本,对于熟悉 Java 和 SWT 的人来说应该不难理解;我很乐意接受 Java 中的答案。
package com.bar.baz
import org.eclipse.swt.widgets._
import org.eclipse.swt.custom.TableEditor
import org.eclipse.swt.layout.FillLayout
import org.eclipse.swt.SWT
object TableTest {
/**
* Launch the application.
*
* @param args
*/
def main(args: Array[String]) = {
try {
val nrows = 5
val ncols = 3
val display = new Display ()
val shell = new Shell (display)
shell.setLayout(new FillLayout())
val table: Table = new Table(
shell, SWT.BORDER /*| SWT.CHECK */| SWT.FULL_SELECTION | SWT.SINGLE
)
val editor: TableEditor = new TableEditor(table)
editor.horizontalAlignment = SWT.LEFT
editor.grabHorizontal = true
table.setLinesVisible(true)
table.setHeaderVisible(true)
for (ii <- 0 until ncols) {
val column: TableColumn = new TableColumn(table, SWT.NONE)
column.setText("Col " + ii.toString)
column.setWidth(40)
}
for (row <- 0 until nrows; col <- 0 until ncols) {
if (col == 0) {
new TableItem(table, SWT.NONE)
createDeleteRowButton(row, col)
}
else {
val item = table.getItem(row)
item.setText(col, s"$row, $col")
}
}
for (col <- 0 until ncols) {
table.getColumn(col).pack()
}
shell.pack ()
shell.open ()
while (!shell.isDisposed) {
if (!display.readAndDispatch ()) display.sleep ()
}
display.dispose ()
def createDeleteRowButton(row: Int, col: Int): Button = {
val delButton = new Button(table, SWT.CHECK)
val item = table.getItem(row)
editor.setEditor(delButton, item, col)
delButton
}
}
catch {
case e: Exception =>
e.printStackTrace()
}
}
}
编辑:我应该包含的一些附加信息。在我的实际应用中,我发现项目以一种非常奇怪的方式环绕:
我必须禁用句点列才能看到这一点。
编辑 2: 添加 Java 代码
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;
public class TableTestJava {
public static void main (String[] args) {
Integer nrows = 5;
Integer ncols = 3;
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
Table table = new Table(
shell, SWT.BORDER /*| SWT.CHECK */| SWT.FULL_SELECTION | SWT.SINGLE
);
TableEditor editor = new TableEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
table.setLinesVisible(true);
table.setHeaderVisible(true);
for (int ii = 0; ii < ncols; ii++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Col " + Integer.toString(ii));
column.setWidth(40);
}
//for (row <- 0 until nrows) {;}
for (int row = 0; row < nrows; row ++) {
for (int col = 0; col < ncols; col ++) {
if (col == 0) {
new TableItem(table, SWT.NONE);
//Inlined function: createDeleteRowButton
Button delButton = new Button(table, SWT.CHECK);
TableItem item = table.getItem(row);
editor.setEditor(delButton, item, col);
//End of inlined function
} else {
TableItem item = table.getItem(row);
item.setText(col, Integer.toString(row) + ", " + Integer.toString(col));
}
}
}
shell.pack ();
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
【问题讨论】:
-
我认为您使用的是 Linux。不是吗?那么您使用的是哪个操作系统。不要将复选框列放在首位。将其设置为第二、第三或最后并检查。将 scala 代码转换为 Java 并发布,以便我可以直接复制并尝试修复代码。
-
谢谢 - 我添加了 Java 代码。实际上是Windows 10。第一列不好是有原因的吗?我还没有尝试过这个建议,但我认为第一行可能有问题,并且通过将所有其他列向下移动一行,在这个演示中取得了一些初步成功,但我还没有在我的应用程序中测试过, 无论如何它都会留下一整行空。
-
一个相关的问题 - 也许我应该稍后再问一个不同的问题?按钮不会随着表格的其余条目滚动;有没有办法让它们适当滚动?
-
@ChandrayyaGK - 我只是尝试更改列;似乎出现了同样的问题。