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.
Related
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);
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
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");
at the moment I'm playing a little bit around with SWT and try to implement an expandbar:
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
ExpandBar bar = new ExpandBar(container, SWT.V_SCROLL);
Composite newConstraint = new Composite(bar, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
newConstraint.setLayout(gridLayout);
Label sourcelbl = new Label(newConstraint, SWT.NONE);
sourcelbl.setImage(getMyImage("source.png"));
sourcelbl.setLayoutData(new GridData(GridData.CENTER, GridData.CENTER,
true, false));
Label spacelbl = new Label(newConstraint, SWT.NONE);
Label targetlbl = new Label(newConstraint, SWT.NONE);
targetlbl.setImage(getMyImage("target.png"));
targetlbl.setLayoutData(new GridData(GridData.CENTER, GridData.CENTER,
true, false));
ListViewer lvSource = new ListViewer(newConstraint);
lvSource.setContentProvider(new ConstraintDialogContentProvider());
lvSource.setLabelProvider(new ConstraintDialogLabelProvider());
lvSource.setInput(fm);
Combo constrainType = new Combo(newConstraint, SWT.NONE);
constrainType.setItems(new String[] { "require", "exclude" });
constrainType.setLayoutData(new GridData(GridData.CENTER,
GridData.CENTER, true, false));
ListViewer lvTarget = new ListViewer(newConstraint);
lvTarget.setContentProvider(new ConstraintDialogContentProvider());
lvTarget.setLabelProvider(new ConstraintDialogLabelProvider());
lvTarget.setInput(fm);
ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0);
item0.setText("New Constraint");
item0.setHeight(newConstraint.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
item0.setControl(newConstraint);
omposite existingConstraints = new Composite(bar, SWT.NONE);
gridLayout = new GridLayout();
gridLayout.numColumns = 2;
existingConstraints.setLayout(gridLayout);
........
ExpandItem item1 = new ExpandItem(bar, SWT.NONE, 1);
item1.setText("Existing Constraints");
item1.setHeight(existingConstraints.computeSize(SWT.DEFAULT,
SWT.DEFAULT).y);
item1.setControl(existingConstraints);
item0.setExpanded(true);
bar.setSpacing(8);
return container;
}
The problen then is the width, which doesn't seems to be computed:
Without the extendbar, everything works fine and the width is automatically computed:
How can I let the extendbar compute the width???
Cheers,
Phil
The ExpandBar size computation only appears to take the size of the ExpandItem text in to account and ignores the size of the control in the item.
You would have to override the ExpandBar.computeSize method to change this (and also ExpandBar.checkSubclass to stop that rejecting your subclass).
Or you could just include spaces at the end of the text for the ExpandItems to fudge the calculated size.
Based on greg-449's answer, I found another workaround, which is to set a widthHint for the ExpandBar:
ExpandBar expandBar = new ExpandBar(this, SWT.BORDER);
GridData gridDataExpandBar = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
gridDataExpandBar.widthHint = 150;
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.