2

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:

Table with missing button circled in red

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:

Application table showing the column of buttons wrapping around

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 ();

    }

}
Community
  • 1
  • 1
bbarker
  • 11,636
  • 9
  • 38
  • 62
  • I think you are on Linux. Isn't it so?Then which OS are you using. Don't make checkbox column firt one. make it second,third or last and check.Convert scala code to Java and post it so that I can directly copy and try to fix the code. – Chandrayya G K Mar 10 '16 at 11:18
  • Thanks - I added the Java code. Actually, it is Windows 10. Is there a reason that the first column is bad? I haven't tried that suggestion yet, but I was thinking that the first row might be problematic, and have had some initial success with this demo by shifting all other columns down one row, but I haven't tested this in my app yet, and it leaves an entire row empty anyway. – bbarker Mar 10 '16 at 14:19
  • A related question - maybe I should ask a different question later? The buttons do not scroll with the rest of the table entries; is there a way to make them scroll appropriately? – bbarker Mar 10 '16 at 16:01
  • @ChandrayyaGK - I did just try changing the column; the same problem seems to occur. – bbarker Mar 10 '16 at 22:12

1 Answers1

0

I'm posting this as an answer, though it isn't ideal, as it adds an empty row and requires a lot of finagling in the real application with indices ... but it works, except the buttons do not move when the rest of the table scrolls vertically. This is the "shift everything down by one" solution.

Note that it requires creating table items ahead of time in a separate loop.

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()
    }
  }

}

It looks like this:

Working example but with empty first row

bbarker
  • 11,636
  • 9
  • 38
  • 62