To clarify, I'm just using SWT - not JFace (trying to learn one GUI library at a time, though perhaps this is not wise). Adding buttons to a table (java swt) gives an example for a TreeEditor, which I'm not using currently.
The problem I'm having is that I can't seem to get a button to show up in row 0, column 0, for the life of me:
One thing I noticed during my debugging is that the check-buttons are shifted down by one; the first visible button in row 1 actually corresponds to item 0.
The code below is in Scala, though it is sufficiently basic that it shouldn't prove difficult to understand for anyone familiar with Java and SWT; I'd happily accept an answer in 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()
}
}
}
Edit: Some additional info that I should have included. In my actual application, I found that the items wrap around in a very strange way:
I had to disable the period column so that I could see this.
Edit 2: Added Java code
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 ();
}
}