Setting header of CheckboxTableViewer - java

The CheckboxTableViewer allows creation a single checklist.
But how do I put the header of such a column since the column itself is not created by a TableColumn.
tableViewer = CheckboxTableViewer.newCheckList(parent, SWT.SINGLE| SWT.BORDER | SWT.FULL_SELECTION);
tableViewer.add(checkListNames.get(0));
tableViewer.add(checkListNames.get(1));
tableViewer.add(checkListNames.get(2));
tableViewer.add(checkListNames.get(3));
tableViewer.add(checkListNames.get(4));
tableViewer.add(checkListNames.get(5));
final Table table = tableViewer.getTable();
table.setLayoutData(new GridData(GridData.FILL_BOTH));
System.out.println(table.getColumnCount()); // this returns a zero
table.setHeaderVisible(true);
table.setLinesVisible(true);

You need to use TableLayout and TableViewerColumn to define a column so you can set the header text.
The minimum code would be:
TableLayout layout = new TableLayout();
TableViewerColumn col = new TableViewerColumn(tableViewer, SWT.LEAD);
col.getColumn().setText("Text");
layout.addColumnData(new ColumnWeightData(100));
table.setLayout(layout);

Related

Java SWT: How to center content of a column in a Table?

I need to center the content of the cells of the fourth column of my table, now they start on left.
This is the table:
Table membersTable = new Table(clubComposite, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
membersTable.setLinesVisible(true);
membersTable.setHeaderVisible(true);
membersTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TableColumn tblclmnName = new TableColumn(membersTable, SWT.NONE);
tblclmnName.setWidth(150);
tblclmnName.setText("Nombre");
TableColumn tblclmnCommonPhoneNumber = new TableColumn(membersTable, SWT.NONE);
tblclmnCommonPhoneNumber.setWidth(120);
tblclmnCommonPhoneNumber.setText("Teléfono");
TableColumn tblclmnCommonMoney = new TableColumn(membersTable, SWT.NONE);
tblclmnCommonMoney.setWidth(150);
tblclmnCommonMoney.setText("Participación Habitual");
TableColumn tblclmnPayed = new TableColumn(membersTable, SWT.CENTER);
tblclmnPayed.setWidth(50);
tblclmnPayed.setText("Pagado");
// populate Table
for (int i=0; i<50; i++) {
TableItem tableItem = new TableItem(membersTable, SWT.CENTER);
tableItem.setText(new String[] {"person "+i, "610610620", "100", ""});
tableItem.setImage(3, uncheckedImage);
}
I tried doing this:
TableColumn tblclmnPayed = new TableColumn(membersTable, SWT.CENTER);
But it seems that it only centers the title of the column, not it's content.
It is possible to achieve my needs on Java SWT?
In order to center the content of a column, you need to specify the SWT.CENTER style bit for the TableItem as well.
For example:
final Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
table.setHeaderVisible(true);
table.setLinesVisible(true);
final TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setText("Column 1");
column1.setWidth(75);
final TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setText("Column 2");
column2.setWidth(75);
final TableColumn column3 = new TableColumn(table, SWT.CENTER);
column3.setText("Column 3");
column3.setWidth(75);
for (int i = 0; i < 5; i++) {
new TableItem(table, SWT.CENTER).setText(new String[] { "a", "b", "c" });
}
Note that this will only affect the contents for the column which also specifies SWT.CENTER, not the content for the other columns.
Edit in regards to centering an image:
I don't believe it is possible to center an image within the table row via the style bits, since they seem to be ignored.
One alternative would be to use a paint listener to draw the image with the correct padding to center the image in the column (See: How to align image to center of table cell (SWT Table)). Note that with this method the row is not resized based on the size of the image, so unless your image is tiny/the same height as the row, you'll have to do some additional work to keep the image form being cut off.

extra column in a JFace TreeViewer

I've a strange problem ...
by creating a (Jface) tree viewer with few column I always got created one more (emtpy) ....
/// TreeViewer creation:
treeViewer = new TreeViewer(parent, SWT.BORDER | SWT.FULL_SELECTION);
/// TreeViewerColumn creation!
for (int column = 0; column < columnsToAdd; column++) {
final TreeViewerColumn treeColumnText = new TreeViewerColumn(treeViewer, SWT.LEFT | SWT.BORDER);
treeColumnText.getColumn().setWidth(columnWidth);
treeColumnText.getColumn().setMoveable(false);
treeColumnText.getColumn().setText(model.getColumnName(column));
treeColumnText.setLabelProvider(columnLProvider[column]);
treeColumnText.getColumn().pack();
}
any idea why?
************* UPDATE
the parent of the treeViewer is the personsTabFolder:
personsTabFolder = new CTabFolder(persons, SWT.NONE);
just to highlight, I don't call any .layout(); on personsTabFolder!
_ _
Kasper
I solved it!
was using a CTabItem instead of a TabItem.

How to reduce table size in Table viewer in RCP?

i have GridTableViewer, in that table showing rows are much.so i planned to reduce table height. But i am unable reduce the tableviewer size of the height.Can u anyone help me in this?
///pasting sample code
#Override
protected void addComponents(FormToolkit toolkit) {
myTableViewr=
new ExtendedGridTableViewer(client, getPage(), SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
Grid grid = myTableViewr.getGrid();
toolkit.adapt(grid, false, false);
grid.setHeaderVisible(true);
grid.setCellSelectionEnabled(false);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
grid.setLayoutData(gridData);
List<ColumnType<?>> columns = new ArrayList<ColumnType<?>>();
columns.add(new nameColumnType());
columns.add(new valueColumnType());
myTableViewr.createColumnGrid(toolkit, columns);
//myModel - my model code
LogoInsertionContentProvider contentProvide = new LogoInsertionContentProvider(this, myTableViewr, myModel);
myTableViewr.setContentProvider(contentProvide);
myTableViewr.setInput(myModel);
}
here, two columns and 3 rows only. but i am getting more than 10 rows
Your GridData for the table is:
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
which is asking to grab all available space. If you want to use a fixed height for the table use something like:
GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
gridData.heightHint = 500; // The height you want

org.eclipse.swt.widgets.Table - add item

I want to add an item to the email column and to the remove column. How do you accomplish this?
I keep looking for a add method or something but I have no been able to find anything.
I have tried setData and redraw after setting some string but this did not work.
Here is the code I have so far:
Table emailTable = new Table(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
FormData fd_table = new FormData();
fd_table.bottom = new FormAttachment(emailText, -3);
fd_table.top = new FormAttachment(0, 10);
fd_table.right = new FormAttachment(emailLabel, 481);
fd_table.left = new FormAttachment(emailLabel, 0, SWT.LEFT);
Table emailTable.setLayoutData(fd_table);
Table emailTable.setHeaderVisible(true);
Table emailTable.setLinesVisible(true);
TableColumn emailColumn = new TableColumn(emailTable, SWT.NONE);
TableColumn emailColumn.setWidth(377);
TableColumn emailColumn.setText("Email");
TableColumn removeColumn = new TableColumn(emailTable, SWT.NONE);
TableColumn removeColumn.setWidth(100);
TableColumn removeColumn.setText("Remove");
You need to create TableItems with first argument in the constructor emailTable and set their text as described in http://www.vogella.com/tutorials/SWT/article.html#swt_table:
TableItem item = new TableItem(emailTable, SWT.NONE);
item.setText (0, "test#example.org");

SWT.Table behaves differently with same code (Eclipse RCP)

Every second line is coloured on the table in the view.
I have the same table twice on the editor, but it doesn't show this separation:
My code of the lower table in the editor:
table1 = new Table(c, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION
| SWT.V_SCROLL);
TableLayout layout1 = new TableLayout();
table1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
layout1.addColumnData(new ColumnWeightData(20, 50, true));
layout1.addColumnData(new ColumnWeightData(20, 50, true));
layout1.addColumnData(new ColumnWeightData(20, 50, true));
layout1.addColumnData(new ColumnWeightData(20, 50, true));
table1.setLayout(layout1);
table1.setLinesVisible(true);
table1.setHeaderVisible(true);
TableColumn colReihe = new TableColumn(table1, SWT.LEFT);
colReihe.setText("Reihe");
TableColumn colPlatz = new TableColumn(table1, SWT.LEFT);
colPlatz.setText("Platz");
TableColumn colPreis = new TableColumn(table1, SWT.LEFT);
colPreis.setText("Preis");
TableColumn colStatus = new TableColumn(table1, SWT.LEFT);
colStatus.setText("verkauft");
this.tableViewer2 = new TableViewer(table1);
this.tableViewer2.setContentProvider(new ArrayContentProvider());
this.tableViewer2.setLabelProvider(new ITableLabelProvider() {
....some more code here....
this.toolkit.adapt(table1, true, true);
I don't really understand your question. Do you expect the table on the lower right of your screenshot to look like the table on the left? If that's the question, check, whether you call toolkit.adapt(table, true, true); with the left table as well, or try to remove that call with the right table. The FormToolkit sets a lot of colors and styles on widgets.

Categories