为了澄清,我只是使用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 ();
}
}
答案 0 :(得分:0)
我发布这个作为答案,虽然它不是理想的,因为它添加了一个空行,并且需要在真实应用程序中使用索引进行大量的重复...但它有效,除了当桌子的其余部分垂直滚动时,按钮不会移动。这就是把所有东西都缩减一个&#34;溶液
请注意,它需要在单独的循环中提前创建表项。
import org.eclipse.swt.widgets._
import org.eclipse.swt.custom.TableEditor
import org.eclipse.swt.layout.FillLayout
import org.eclipse.swt.SWT
object TableTest {
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) {new TableItem(table, SWT.NONE)}
for (row <- 0 until nrows; col <- 0 until ncols) {
if (col == 0) {
createDeleteRowButton(row, col)
}
else if (row < nrows - 1) {
val item = table.getItem(row+1)
item.setText(col, s"$row, $col")
}
}
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()
}
}
}
看起来像这样: