SWT - show multiple table items - java

Could anybody help me?
I have a table in SWT and simply I want to show some table items but the table only shows one item and the others with vertical scroll. I want to show everything, without scrolling. I tried with the option SWT_NO_SCROLL but is not working. I have a method that creates the table and other that populates it creating a new table item, they´re working good, the problem is that only shows the first item and the other are scrolling.
My code is:
private void createTable(Composite parent){
table = new Table (parent, SWT.BORDER);
TableColumn tcFile = new TableColumn(table, SWT.LEFT);
TableColumn tcStatus = new TableColumn(table, SWT.LEFT);
tcFile.setText("File");
tcStatus.setText("Status");
tcFile.setWidth(500);
tcStatus.setWidth(500);
table.setVisible(true);
table.setHeaderVisible(true);
}
private void populateTable(String file, String status){
TableItem item = new TableItem(table, SWT.LEFT);
item.setText(new String[] { file, status});
}
Composite top = new Composite(parent, SWT.WRAP);
GridLayout layout = new GridLayout();
layout.marginHeight = -5;
layout.marginWidth = 0;
top.setLayout(layout);
Composite banner = new Composite(top, SWT.WRAP);
banner.setLayoutData(new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_BEGINNING, false, false));
layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 10;
layout.numColumns = 5;
banner.setLayout(layout);
createTable(top);

You haven't specified any layout data for the Table, try something like:
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(data);
If you don't have anything else that is setting the dialog/window size you may need to specify a table height hint:
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 200; // Vertical size for table
table.setLayoutData(data);

Related

How to create a Java SWT table with multiple columns without giving static column width

I'm currently developing an eclipse plugin and in that plugin, there is a form view as a design template. In that form view, I have added a Table and there should have two columns in width ratio of 1:2. And also I want that table to be responsive and dynamically change its column width in order to the formView page width.
The following code segment is the one I'm currently using.
Table table = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
fd = new FormData();
fd.height = 200;
fd.top = new FormAttachment(removeTestCaseButton, 5);
fd.left = new FormAttachment(1);
fd.right = new FormAttachment(99);
table.setLayoutData(fd);
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableColumn column1 = new TableColumn(testCaseTable, SWT.CENTER);
column.setText("column One");
TableColumn column2 = new TableColumn(testCaseTable, SWT.CENTER);
column2.setText("column Two");
form.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
Rectangle area = form.getBody().getClientArea();
int width = area.width;
column1.setWidth(width / 3);
column1.setWidth(width * 2 / 3);
}
});
But here the problem is when I open the FormView it works fine. But my table is inside a Section. Once I expand or collapse the Section the table width is getting increased with appearing the horizontal scrollbar.
I just want a solid solution for this.
This is much easier to do using the JFace TableViewer with the TableColumnLayout and ColumnWeightData, but you will have to rework your code to use JFace style content and label providers for the table.
TableColumnLayout tableLayout = new TableColumnLayout();
// A separate composite containing just the table viewer is required
Composite tableComp = new Composite(parent, SWT.NONE);
tableComp.setLayout(tableLayout);
TableViewer viewer = new TableViewer(tableComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
TableViewerColumn col1 = new TableViewerColumn(viewer, SWT.LEAD);
col1.getColumn().setText("Column 1");
col1.setLabelProvider(.... label provider for column 1 ....);
// Weight for column
tableLayout.setColumnData(col1.getColumn(), new ColumnWeightData(60));
TableViewerColumn col2 = new TableViewerColumn(viewer, SWT.LEAD);
col2.getColumn().setText("Column 2");
col2.setLabelProvider(....... label provider for column 2 .....);
// Weight for column
tableLayout.setColumnData(col2.getColumn(), new ColumnWeightData(40));
viewer.getTable().setHeaderVisible(true);
viewer.getTable().setLinesVisible(true);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setInput(.... input data for the viewer ....);

How to remove the spacing between rows in a GridLayout composite?

these are just sample codes. I created a GridLayout composite and removed the margins. But there are still spaces in between the rows.
Composite composite = new Composite(shell, SWT.NONE);
GridLayout gl_composite = new GridLayout(1, false);
gl_composite.marginWidth = 0;
gl_composite.marginHeight = 0;
composite.setLayout(gl_composite);
Composite composite_1 = new Composite(composite, SWT.BORDER);
GridData gd_composite_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_composite_1.heightHint = 52;
gd_composite_1.widthHint = 802;
composite_1.setLayoutData(gd_composite_1);
composite_1.setLayout(new GridLayout(1, false));
Composite composite_2 = new Composite(composite, SWT.BORDER);
GridData gd_composite_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
gd_composite_2.widthHint = 803;
composite_2.setLayoutData(gd_composite_2);
I need the rows to not have any spaces in between. The composites are bordered since I need to have a line below . I'm new to SWT so..
Thanks!!
The verticalSpacing field of GridLayout sets the spacing between rows, so:
gl_composite.verticalSpacing = 0;
Use horizontalSpacing for spacing between columns.
The default for both values is 5

How to use two Grid Layouts in one Composite

I have some trouble with Composite. I have written the following code:
final Composite mavniOptionsComposite = new Composite(parent, SWT.NONE);
mavniOptionsComposite.setLayout(new GridLayout(3, false));
final GridData textGridData = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
final Label mavniFormatingLabel = new Label(mavniOptionsComposite, SWT.None);
mavniFormatingLabel.setText(PFT.MAVNI_HISTORY.getExplanationLabelText());
mavniFormatingLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
final GridData mavniFormatingGridData = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
final Button mavniHistoryRunCheckbox = new Button(mavniOptionsComposite, SWT.CHECK);
mavniHistoryRunCheckbox.setLayoutData(mavniFormatingGridData);
mavniHistoryRunCheckbox.setText(PFT.MAVNI_HISTORY.getExplanationLabelText());
mavniHistoryRunCheckbox.addSelectionListener(new mavniHistoryCheckBoxSelectionAdapter());
widgetsMap.put(PFT.MAVNI_HISTORY, mavniHistoryRunCheckbox);
final Button mavniRealtimeCheckbox = new Button(mavniOptionsComposite, SWT.CHECK);
mavniRealtimeCheckbox.setLayoutData(mavniFormatingGridData);
mavniRealtimeCheckbox.setText(PFT.MAVNI_REALTIME.getExplanationLabelText());
mavniRealtimeCheckbox.setEnabled(MyPreferences.isWmcDefined());
widgetsMap.put(PFT.MAVNI_REALTIME, mavniRealtimeCheckbox);
final Label toolNameLabel = new Label(mavniOptionsComposite, SWT.NONE);
toolNameLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
toolNameLabel.setText(PFT.TOOL_NAME.getExplanationLabelText());
final Text toolNameText = new Text(mavniOptionsComposite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
toolNameText.setLayoutData(textGridData);
widgetsMap.put(PFT.TOOL_NAME, toolNameText);
final Label toolVersionLabel = new Label(mavniOptionsComposite, SWT.NONE);
toolVersionLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
toolVersionLabel.setText(PFT.TOOL_VERSION.getExplanationLabelText());
final Text toolVersionText = new Text(mavniOptionsComposite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
toolVersionText.setLayoutData(textGridData);
widgetsMap.put(PFT.TOOL_VERSION, toolVersionText);
mavniOptionsComposite.setLayout(new GridLayout(2, false));
new MavniOptionViewer(MyPreferences.getMavniOptionsList()).createViewer(mavniOptionsComposite);
createExpandItem(parent, mavniOptionsComposite, "Mavni Options", com.mavni.ui.Activator
.getDefault().getImageRegistry().get(IImageKeys.EXECUTE.getPath()), false);
But I get the following behavior:
I would like the table to expand to on the full layout. As I understand that happens because I set setLayout(new GridLayout(3, false)); and the table contains only two columns. If I set mavniOptionsComposite.setLayout(new GridLayout(2, false)); then the table works as expected but the other elements are showing no in there place (like it should be now).
The wanted behviour:
At first I though to create two GridLayouts and insert them into one composite. I tried to search for a way to combine two layouts in one composite but could not find a way. I need one composite because createExpandItem works with only one composite. How to solve it?
EDIT: In the MavniOptionViewer I have:
public void createViewer(final Composite parent) {
commandsTable = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION);
commandsTable.setHeaderVisible(true);
commandsTable.setLinesVisible(true);
final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 5);
data.heightHint = 120;
commandsTable.setLayoutData(data);
final Map<ButtonTypeEnum, Button> buttons = ButtonComboFactory.createAddRemoveCombo(parent);
final Button add = buttons.get(ButtonTypeEnum.ADD);
final Button remove = buttons.get(ButtonTypeEnum.REMOVE);
...
I though of another possible solution. Is it possbile to change createViewer so the table will have 3 columns and the add remove buttons will be on seprated line. How to do it?
A Composite can only have one layout, you can't change it halfway. It will just end up using the last layout you set for everything.
You could use multiple Composites in a containing Composite.
You could keep your main Composite with a GridLayout of 3 columns and tell the table to occupy all 3 of these columns instead of 2 like you are doing now.
You can do that by changing the horizontal span of the table GridData from 2 to 3:
final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 5);
Also, you should not reuse GridData objects; from the documentation:
Every control in a Composite that is managed by a GridLayout must have
a unique GridData object.

How to design a Composite in SWT

I want to put 3 fixed size group in the composite in A side. And I want to place the image and the label middle of this group. My example image and code is below. The problem is the groups are resize according to label size in them and labels were written top of the group, but I want the place groups as equal fixed size to cover width of A side and labels should be vertically middle of the group.
private void designComposite() {
Section sectionA = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
sectionA.setText("A");
Composite sectionClientA = toolkit.createComposite(sectionA);
sectionClientA.setLayout(new RowLayout());
Composite dynamicDataComp = toolkit.createComposite(sectionClientA);
dynamicDataComp.setLayout(new RowLayout());
Group group_incom = new Group(dynamicDataComp, SWT.NONE);
group_incom.setLayout(new RowLayout());
Label lbl_img_incom = new Label(group_incom, SWT.CENTER);
Image img_incom = new Image(lbl_img_incom.getDisplay(),
"<path>");
lbl_img_incom.setImage(img_incom);
group_incom.setText("# of Incoming Messages :");
Label lbl_incomMsg = toolkit.createLabel(group_incom, "99", SWT.CENTER | SWT.VERTICAL);
Font incomFont = new Font(lbl_incomMsg.getDisplay(), new FontData("Arial", 12, SWT.BOLD));
lbl_incomMsg.setFont(incomFont);
lbl_incomMsg.pack();
Group group_outgo = new Group(dynamicDataComp, SWT.NONE);
group_outgo.setLayout(new RowLayout());
Label lbl_img_outgo = new Label(group_outgo, SWT.CENTER);
Image img_outgo = new Image(lbl_img_outgo.getDisplay(),
"<path>");
lbl_img_outgo.setImage(img_outgo);
group_outgo.setText("# of Outgoing Messages :");
Label lbl_outgoMsg = toolkit.createLabel(group_outgo, "145639612", SWT.CENTER);
Font outgoFont = new Font(lbl_outgoMsg.getDisplay(), new FontData("Arial", 13, SWT.BOLD));
lbl_outgoMsg.setFont(outgoFont);
lbl_outgoMsg.pack();
Group group_error = new Group(dynamicDataComp, SWT.NONE);
group_error.setLayout(new RowLayout());
Label lbl_img_error = new Label(group_error, SWT.CENTER);
Image img_error = new Image(lbl_img_error.getDisplay(),
"<path>");
lbl_img_error.setImage(img_error);
group_error.setText("# of Error Messages :");
Label lbl_errorMsg = toolkit.createLabel(group_error, "345", SWT.CENTER);
Font errorFont = new Font(lbl_errorMsg.getDisplay(), new FontData("Arial", 13, SWT.BOLD));
lbl_errorMsg.setFont(errorFont);
lbl_errorMsg.pack();
sectionA.setClient(sectionClientA);
}
Use the GridLayout and set the columns equal width:
Composite dynamicDataComp = new Composite(parent, SWT.NONE);
dynamicDataComp.setLayout(new GridLayout(3, true));
dynamicDataComp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Group group_incom = new Group(dynamicDataComp, SWT.NONE);
group_incom.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
group_incom.setLayout(new RowLayout());
//...
One way is to use GridLayout for the parent composite and tell it use 3 equal sized columns:
Composite dynamicDataComp = toolkit.createComposite(sectionClientA);
dynamicDataComp.setLayout(new GridLayout(3, true));

How to get the text content on the swt table with arbitrary controls

I have different controls placed on a table using TableEditor.
...
TableItem [] items = table.getItems ();
for (int i=0; i<items.length; i++) {
TableEditor editor = new TableEditor (table);
final Text text1 = new Text (table, SWT.NONE);
text1.setText(listSimOnlyComponents.get(i).getName());
text1.setEditable(false);
editor.grabHorizontal = true;
editor.setEditor(text1, items[i], 0);
editor = new TableEditor (table);
final CCombo combo1 = new CCombo (table, SWT.NONE);
combo1.setText("");
Set<String> comps = mapComponentToPort.keySet();
for(String comp:comps)
combo1.add(comp);
editor.grabHorizontal = true;
editor.setEditor(combo1, items[i], 1);
} //end of for
...
When I try to get the text on the table using getItem(i).getText, I get empty string
...
TableItem [] items = table.getItems ();
for(int i=0; i<items.length; i++) {
TableItem item = items[i];
String col0text = items[i].getText(0); //this text is empty
String col1text = items[i].getText(1); //this text is empty
}
...
Why does getText returns empty strings even when I have text appearing on the table?
Instead of using the text property, you might consider using the data property instead.
Benefits:
you can attach the data (e.g. a control or complex data structure) directly to the table item.
the only thing your table item creation need to know from your table cell editor is the object reference to be stored in the data property.
you don't need that event handling stuff (just read the controls data when you really need it).
Create:
TableItem item = new TableItem(table, SWT.None);
TableEditor editor = new TableEditor(table);
Button checkbox = new Button(table, SWT.CHECK);
checkbox.pack();
editor.setEditor(checkbox,item,0);
item.setData("cb",checkbox); // using key enables you to add more pieces of complex data
Read:
for (TableItem item : table) {
Button checkbox = (Button) item.getData("cb");
if (checkbox.getSelection()) { /* ... do whatever you want */ }
}
When the table shows up, the checkbox is visible and can be clicked. Using the setText method fails in case of transparent background controls -> you will see the text of the table items cell below
your control (I tried this).
Anyway it would be much easier if it would be possible to extend the table item class to hide even the data key. But as usual, subclassing is denied.
in the event listeners for the controls I added
item.setText call
...
combo1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent evt) {
String sel = combo2.getText();
item.setText(ComponentPortToConnectCol, sel);
}});
...
This gives me the desired result. Thanks OTisler for the clue
How are you setting up your table? I copied your code to debug it and set up the table as shown below (first for loop)
I tried but couldn't reproduce the problem you're seeing. It might be the way you are adding things to the table.
TableEditor Examples
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
for (int i = 0; i < 3; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "" + i, "" + i, "" + i });
}
TableItem [] items = table.getItems();
for (int i=0; i<items.length; i++) {
TableEditor editor = new TableEditor (table);
final Text text1 = new Text (table, SWT.NONE);
text1.setText("TEST");
text1.setEditable(false);
editor.grabHorizontal = true;
editor.setEditor(text1, items[i], 0);
editor = new TableEditor (table);
final CCombo combo1 = new CCombo (table, SWT.NONE);
combo1.setText("");
String[] comps = {"A","B","C"};
for(String comp:comps)
combo1.add(comp);
editor.grabHorizontal = true;
editor.setEditor(combo1, items[i], 1);
} //end of for
TableItem [] items2 = table.getItems ();
for(int i=0; i<items2.length; i++) {
TableItem item = items2[i];
String col0text = items2[i].getText(0); //returns '0' first for loop
}

Categories

Resources