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

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

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.

GUI in design page and GUI when RUN is not the same (Eclipse)

I want to manage the position of component in GUI by easily drag and drop them at design page of Eclipse. This is the GUI I see in design page.(Right click>Test/Preview)
I think after I finish rearrange the component in design page, it will look similar when I RUN the app. But, this GUI appear.
The different appearance make me very hard to adjust the component, for example the width of DAY 2, the height of Medication, DOB : and the green background.
Please let me know if there is any solution to this problem. Thanks.
You can use the GridLayout where each day is a column and the subjects(diagnosis, treatment and so on) are the rows. And you create a composite in each grid cell (like Day1 & diagnosis) which contains your buttons for this day and the subject.
[EDIT]
My suggested implementation is: (SWT.BORDER marks all cells of the toplevel grid)
GridLayout topLevelLayout = new GridLayout();
topLevelLayout.numColumns = 4;
parent.setLayout(topLevelLayout);
// head row
Label label = new Label(parent, SWT.BORDER);
label.setText("Activity");
label = new Label(parent, SWT.BORDER);
label.setText("Day 1");
label = new Label(parent, SWT.BORDER);
label.setText("Day 2");
label = new Label(parent, SWT.BORDER);
label.setText("Day 3");
// new row - first cell
label = new Label(parent, SWT.BORDER);
label.setText("Diagnosis");
// Day1 & Diagnosis
GridLayout cellLayout = new GridLayout();
cellLayout.numColumns = 2;
Composite composite = new Composite(parent, SWT.BORDER);
composite.setLayout(cellLayout);
Button button = new Button(composite, SWT.NONE);
button.setText("ECG");
button = new Button(composite, SWT.NONE);
button.setText("Blood Pressure");
button = new Button(composite, SWT.NONE);
button.setText("Vital signs");
// other subjects of diagnosis at day 1...
// Day2 & Diagnosis
composite = new Composite(parent, SWT.BORDER);
// same layout like for day1 & diagnosis
composite.setLayout(cellLayout);
button = new Button(composite, SWT.NONE);
button.setText("ECG");
button = new Button(composite, SWT.NONE);
button.setText("Labs");
button = new Button(composite, SWT.NONE);
button.setText("Blood pressure");
// other subjects of diagnosis at day 2...
// Day3 & Diagnosis
composite = new Composite(parent, SWT.BORDER);
// same layout like for day1 & diagnosis
composite.setLayout(cellLayout);
button = new Button(composite, SWT.NONE);
button.setText("Stress Tests");
button = new Button(composite, SWT.NONE);
button.setText("Labs");
button = new Button(composite, SWT.NONE);
button.setText("Cardiac rhythm");
// other subjects of diagnosis at day 1...
label = new Label(parent, SWT.BORDER);
label.setText("Treatment");
Things like this happen with any designer. Usually they are an indicator of a location change occuring at runtime as the result of a dynamic parameter of some kind. Make sure that you having the correct padding on each of the buttons, and whatever you are using to contain the buttons in should be using a consistent spacing for each one. Also make sure that alignment within the containers is correct, and that both your buttons and the containers they are in are properly anchored.

Setting header of CheckboxTableViewer

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

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.

selection in CheckboxTableViewer on win7 is not working

i try to use an org.eclipse.jface.viewers.CheckboxTableViewer, as a component of a org.eclipse.jface.wizard.WizardPage. I created it this way:
public void createControl(Composite parent) {
composite = new Composite(parent, SWT.NULL);
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
composite.setLayout(gridLayout);
setControl(composite);
/* CheckboxTableViewer */
viewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
final Table table = viewer.getTable();
GridData data1 = new GridData();
data1.grabExcessHorizontalSpace = true;
data1.grabExcessVerticalSpace = true;
data1.horizontalSpan = 2;
data1.horizontalAlignment = SWT.FILL;
data1.verticalAlignment = SWT.FILL;
table.setLayoutData(data1);
table.setHeaderVisible(true);
table.setLinesVisible(true);
checkboxColumn = new TableColumn(table, SWT.LEFT);
...
the content of the viewer is inserted dynamically by a contentprovider. Everything works fine on gnome. While testing this on windows 7 (64 and 32 bit also), i am not able to select any entries of that view. Mouseclicks just seems to have no impact on the view.
I added a mouselistener to the table, and the mouseUp-/Down event is fired, selectionChanged and doubleClick on the viewer is not fired. Anyone who can explain this behaviour to me?
thx in advance,
hage
(i already posted this question in the eclipse forum without any response yet: http://www.eclipse.org/forums/index.php/t/250953/ )
You have to add another style flag while creating the CheckboxTableViewer: SWT.FULL_SELECTION
viewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER | SWT.FULL_SELECTION);
You can now select rows in the table by a single clicking.

Categories