faraday
faraday

Reputation: 324

Inserting a Button to JFace Table

How can I insert a SWT Button control into JFace TableViewer ?

Upvotes: 2

Views: 6633

Answers (2)

Kirby
Kirby

Reputation: 121

The answer given is nice a nice way to implement your own buttons with custom drawings, in or outside the a table. However, you can put SWT controls in JFace Tables.

http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/PlacearbitrarycontrolsinaSWTtable.htm

The solution for building a table with columns containing comboboxes, text fields, and buttons provided by the link is:

Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setLinesVisible(true);
for (int i = 0; i < 3; i++) {
  TableColumn column = new TableColumn(table, SWT.NONE);
  column.setWidth(100);
}
for (int i = 0; i < 12; i++) {
  new TableItem(table, SWT.NONE);
}
TableItem[] items = table.getItems();
for (int i = 0; i < items.length; i++) {
  TableEditor editor = new TableEditor(table);
  CCombo combo = new CCombo(table, SWT.NONE);
  editor.grabHorizontal = true;
  editor.setEditor(combo, items[i], 0);
  editor = new TableEditor(table);
  Text text = new Text(table, SWT.NONE);
  editor.grabHorizontal = true;
  editor.setEditor(text, items[i], 1);
  editor = new TableEditor(table);
  Button button = new Button(table, SWT.CHECK);
  button.pack();
  editor.minimumWidth = button.getSize().x;
  editor.horizontalAlignment = SWT.LEFT;
  editor.setEditor(button, items[i], 2);
}

Upvotes: 12

Alexey Romanov
Alexey Romanov

Reputation: 170713

You can't. More generally, you can't insert any widgets in tables and trees in SWT, because not all platforms support it. What you can do instead is

  1. Take two screenshots of the button in normal and clicked states;

  2. Put the normal screenshot in table as an image;

  3. Handle clicks on the TableItem.

Here is an example for checkboxes: http://tom-eclipse-dev.blogspot.com/2007/01/tableviewers-and-nativelooking.html

Upvotes: 2

Related Questions