How to always show vertical scroll bar in SWT table? - java

Is it possible to always show the vertical scroll bar in a SWT table even if the table is empty? By always showing a (possible disabled) vertical scroll bar one can avoid that the last column get partially hidden when the columns use ColumnWeightData for layouting.
I tried to initialize the table with SWT.V_SCROLL or to use table.getVerticalBar().setVisible(true) - both without success.
There is a method setAlwaysShowScrollBars in ScrollableComposite. What I am looking for is a similar method in Table.
UPDATE: I suppose that the scroll bars which are visible when the table contains enough data are not those scroll bars which Table inherits from Scrollable. I have debugged ScrollBar.setVisible(boolean) and it seems not be called on table layout updates. Is this observation correct?
UPDATE 2: Here is a snippet for a table construction. It would be great to have the vertical scrollbar visible even if the table is empty and to have the column headers visible even if the table data are scrolled down. Note: The snippet has left out some details as the label provider and some other controls arranged at the same parent composite.
protected void createMasterPart(final IManagedForm managedForm, Composite parentComposite)
{
FormToolkit toolkit = managedForm.getToolkit();
Composite contentComposite = toolkit.createComposite(parentComposite, SWT.NONE);
contentComposite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
toolkit.paintBordersFor(contentComposite);
contentComposite.setLayout(new GridLayout(2, false));
GridData gd;
Composite tableComposite = new Composite(contentComposite, SWT.NONE);
TableColumnLayout tableColumnLayout = new TableColumnLayout();
tableComposite.setLayout(tableColumnLayout);
gd = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 3);
tableComposite.setLayoutData(gd);
speakerTableViewer = new TableViewer(tableComposite, SWT.BORDER | SWT.FULL_SELECTION);
speakerTableViewer.setContentProvider(ArrayContentProvider.getInstance());
Table speakerTable = speakerTableViewer.getTable();
speakerTable.setHeaderVisible(true);
speakerTable.setLinesVisible(true);
toolkit.paintBordersFor(speakerTable);
TableViewerColumn tableViewerAudiosampleColumn = new TableViewerColumn(speakerTableViewer, SWT.NONE);
TableColumn audiosampleColumn = tableViewerAudiosampleColumn.getColumn();
tableColumnLayout.setColumnData(audiosampleColumn, new ColumnWeightData(60, true));
audiosampleColumn.setText("Sample");
TableViewerColumn tableViewerSpeakerColumn = new TableViewerColumn(speakerTableViewer, SWT.NONE);
TableColumn speakerColumn = tableViewerSpeakerColumn.getColumn();
tableColumnLayout.setColumnData(speakerColumn, new ColumnWeightData(60, true));
speakerColumn.setText("Speaker");
TableViewerColumn tableViewerRemarkColumn = new TableViewerColumn(speakerTableViewer, SWT.NONE);
TableColumn remarkColumn = tableViewerRemarkColumn.getColumn();
tableColumnLayout.setColumnData(remarkColumn, new ColumnWeightData(120, true));
remarkColumn.setText("Remark");
}

It's not possible to force the Table to always show scroll bars, the OS decides when to show them.
Alternatives
Right, I came up with a solution very similar to my answer to this question:
Is it possible to get the vertical/horizontal scroll bar visible when the SWT List is in disabled state?
The idea is to use a ScrolledComposite (as the other answer already suggested) to take care of the scrolling. The Table itself won't scroll. However, this won't make any difference, because the user won't be able to tell the difference.
ScrolledComposite has a method called setAlwaysShowScrollBars(boolean) with which you can force it to always show the scroll bars, even if they aren't required.
Here is some sample code, that will illustrate what I just talked about:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final ScrolledComposite composite = new ScrolledComposite(shell, SWT.V_SCROLL);
composite.setLayout(new GridLayout());
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Table table = new Table(composite, SWT.NO_SCROLL | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
composite.setContent(table);
composite.setExpandHorizontal(true);
composite.setExpandVertical(true);
composite.setAlwaysShowScrollBars(true);
composite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Button fillTable = new Button(shell, SWT.PUSH);
fillTable.setText("Fill table");
fillTable.setLayoutData(new GridData(SWT.FILL, SWT.END, true, false));
fillTable.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
if (table.getColumnCount() < 1)
{
for (int col = 0; col < 4; col++)
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Column " + col);
}
}
for (int row = 0; row < 20; row++)
{
TableItem item = new TableItem(table, SWT.NONE);
for (int col = 0; col < table.getColumnCount(); col++)
{
item.setText(col, "Item " + row + " " + col);
}
}
for (int col = 0; col < table.getColumnCount(); col++)
{
table.getColumn(col).pack();
}
composite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
});
Button clearTable = new Button(shell, SWT.PUSH);
clearTable.setText("Clear table");
clearTable.setLayoutData(new GridData(SWT.FILL, SWT.END, true, false));
clearTable.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
table.removeAll();
composite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
});
shell.pack();
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Looks like this:
As you can see, the scroll bar is always visible.
UPDATE
As pointed out in the comment, this approach will not keep the Table headers visible when you scroll down. If you could post a small working code example that illustrates your problem, we might come up with an alternative (unrelated to forcing the scroll bars).
UPDATE2
Here is some code that should do what you want, the trick is to trigger a resize event on the parent of the TableViewer, the horizontal scrollbar that is shown isn't really necessary and it disappears after you resize the window:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout());
createMasterPart(shell);
shell.pack();
shell.setSize(400, 300);
shell.open();
shell.layout(true, true);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void createMasterPart(Composite parentComposite)
{
Composite composite = new Composite(parentComposite, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));
Composite tableComposite = new Composite(composite, SWT.NONE);
TableColumnLayout tableColumnLayout = new TableColumnLayout();
tableComposite.setLayout(tableColumnLayout);
tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
TableViewer tableViewer = new TableViewer(tableComposite, SWT.BORDER | SWT.FULL_SELECTION);
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
Table table = tableViewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableViewerColumn firstTableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn firstTableColumn = firstTableViewerColumn.getColumn();
firstTableColumn.setText("Sample");
firstTableViewerColumn.setLabelProvider(new ColumnLabelProvider()
{
#Override
public String getText(Object element)
{
Dummy p = (Dummy) element;
return p.first;
}
});
TableViewerColumn secondTableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn secondTableColumn = secondTableViewerColumn.getColumn();
secondTableColumn.setText("Speaker");
secondTableViewerColumn.setLabelProvider(new ColumnLabelProvider()
{
#Override
public String getText(Object element)
{
Dummy p = (Dummy) element;
return p.second;
}
});
TableViewerColumn thirdTableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn thirdTableColumn = thirdTableViewerColumn.getColumn();
thirdTableColumn.setText("Remark");
thirdTableViewerColumn.setLabelProvider(new ColumnLabelProvider()
{
#Override
public String getText(Object element)
{
Dummy p = (Dummy) element;
return p.third;
}
});
List<Dummy> elements = new ArrayList<>();
for(int i = 0; i < 20; i++)
{
elements.add(new Dummy("firstfirstfirst " + i, "secondsecondsecond " + i, "thirdthirdthirdthirdthirdthird " + i));
}
tableViewer.setInput(elements);
tableColumnLayout.setColumnData(firstTableColumn, new ColumnWeightData(1, true));
tableColumnLayout.setColumnData(secondTableColumn, new ColumnWeightData(1, true));
tableColumnLayout.setColumnData(thirdTableColumn, new ColumnWeightData(2, true));
}
private static class Dummy
{
public String first;
public String second;
public String third;
public Dummy(String first, String second, String third)
{
this.first = first;
this.second = second;
this.third = third;
}
}

I have created a solution that I think is better than put your table inside a ScrolledComposite.
My solution: fill my table with empty items until my scroll bar is visible.
Example:
// Flag that knows if the empty item was added or not
boolean addedEmptyItem = false;
// Get the table client area
Rectangle rect = table.getClientArea ();
// Get the item height
int itemHeight = table.getItemHeight ();
// Get the header height
int headerHeight = table.getHeaderHeight ();
// Calculate how many items can be visible without scrolling
int visibleCount = (rect.height - headerHeight + itemHeight - 1) / itemHeight;
while ( visibleCount > table.getItemCount() ) {
// Add an empty item
new TableItem( table, SWT.NONE );
// Set the flag
addedEmptyItem = true;
}
// Vertical bar é disabled if an empty item was added
table.getVerticalBar().setEnabled( !addedEmptyItem );
I hope this solution helps someone.
Thanks.

I don't think you can do this but you can try call ScrolledComposite.setAlwaysShowScrollbars() to true, but you will see both of the enabled scrollbars all the time.

Related

swt menu on click hide display composites

I am building a SWT application and have a menu created. Menu has multiple menu items like Add, Edit, Help. On click of each Menu Item, I want to show a composite which will display the details of it. I am able to build it, problem I am facing is, the space of hidden composite is not taken by visible composite. How can we make the composite occupy the entire space.
Also I am adding the selection listener to make the current composite visible and other composite hidden. In the current app there will multiple menu items and each one will have composite associated it. Listener needs reference of all composites to make them visible/hidden. Is there any better approach to do this.
public class MenuToggle {
boolean startup = true;
Menu menu, fileMenu, helpMenu;
Composite composite1,composite2;
public MenuToggle(Shell shell) {
createMenu(shell);
createFileView(shell);
createHelpView(shell);
startup = false;
}
public void createMenu(Shell shell) {
//Menu Bar
menu = new Menu(shell, SWT.BAR);
//File Menu
fileMenu = new Menu(shell, SWT.DROP_DOWN);
MenuItem fileMenuHeader = new MenuItem(menu, SWT.CASCADE);
fileMenuHeader.setText("&File");
fileMenuHeader.setMenu(fileMenu);
MenuItem fileSaveItem = new MenuItem(fileMenu, SWT.PUSH);
fileSaveItem.setText("&Save");
MenuItem fileExitItem = new MenuItem(fileMenu, SWT.PUSH);
fileExitItem.setText("E&xit");
//Help Menu
helpMenu = new Menu(shell, SWT.DROP_DOWN);
MenuItem helpMenuHeader = new MenuItem(menu, SWT.CASCADE);
helpMenuHeader.setText("&Help");
helpMenuHeader.setMenu(helpMenu);
MenuItem helpGetHelpItem = new MenuItem(helpMenu, SWT.PUSH);
helpGetHelpItem.setText("&Get Help");
shell.setMenuBar(menu);
fileSaveItem.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
composite1.setVisible(true);
((GridData)composite1.getLayoutData()).exclude = false;
composite2.setVisible(false);
((GridData)composite2.getLayoutData()).exclude = true;
composite2.layout(true, true);
}
});
helpGetHelpItem.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
composite1.setVisible(false);
((GridData)composite1.getLayoutData()).exclude = true;
composite2.setVisible(true);
((GridData)composite2.getLayoutData()).exclude = false;
composite2.layout(true, true);
}
});
}
public void createFileView(Shell shell) {
composite1 = new Composite(shell, SWT.BORDER);
composite1.setVisible(true);
GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true);
composite1.setLayoutData(gd1);
composite1.setLayout(new GridLayout(1,true));
Label label = new Label(composite1, SWT.CENTER);
label.setBounds(composite1.getClientArea());
label.setText("Saved");
}
public void createHelpView(Shell shell) {
composite2 = new Composite(shell, SWT.BORDER);
composite2.setVisible(false);
GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true);
composite2.setLayoutData(gd2);
composite2.setLayout(new GridLayout(1,true));
Label label1 = new Label(composite2, SWT.CENTER);
label1.setBounds(composite2.getClientArea());
label1.setText("No worries!");
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Menu Display");
MenuToggle instance = new MenuToggle(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
There are a number of issues here.
You are using FillLayout for the Shell layout, so the GridData you are setting on the composites is ignored. You must use GridLayout for the Shell:
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout()); // Changed
When you change the exclude settings you must call layout on the parent of the composite - the shell:
fileSaveItem.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(final SelectionEvent e) {
composite1.setVisible(true);
((GridData)composite1.getLayoutData()).exclude = false;
composite2.setVisible(false);
((GridData)composite2.getLayoutData()).exclude = true;
shell.layout(true, true); // change
}
});
helpGetHelpItem.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(final SelectionEvent e) {
composite1.setVisible(false);
((GridData)composite1.getLayoutData()).exclude = true;
composite2.setVisible(true);
((GridData)composite2.getLayoutData()).exclude = false;
shell.layout(true, true); // change
}
});
You are calling setBounds on the Label controls, this does not work when you are using layouts because the layout also calls setBounds and overrides your settings, use setLayoutData instead
Label label = new Label(composite1, SWT.CENTER);
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); // replace
//label.setBounds(composite1.getClientArea()); // wrong
As for dealing with lots of Composite controls you could call shell.getChildren and loop through the child controls. Or add the composites to a List and loop through that.

How to configure this GridLayout correctly?

I'm sadly far from being an expert in SWT and RCP, but I really tried my best here... I can't figure out how to configure the widgets to get this layout (just a Photoshopped screen, never worked this way):
This is what I get if I set the column number of the GridLayout to 2:
Here is the Refresh and the Blacklist button in the wrong row, but at least everything is visible...
And this is what I get if I set the column number of the GridLayout to 3:
This is total messed up... Most of the widgets are pushed outside the visible area. DatePicker, Refresh, Whitelist and the Calculate buttons are not visible, they are somewhere outside on the right.
This is the codepart for this screen area:
resultingProductsGroup = new Group(propProdGroup, SWT.NONE);
final GridData gd_resultingProductsGroup = new GridData(SWT.FILL,
SWT.CENTER, true, false);
gd_resultingProductsGroup.widthHint = 240;
resultingProductsGroup.setLayoutData(gd_resultingProductsGroup);
resultingProductsGroup.setText("Resulting products");
final GridLayout gridLayout_4 = new GridLayout();
gridLayout_4.numColumns = 2;
resultingProductsGroup.setLayout(gridLayout_4);
Label refDateLabel = new Label(resultingProductsGroup, SWT.NONE);
refDateLabel.setText("Reference date:");
refDateInput = new DateInput(resultingProductsGroup, SWT.BORDER);
refDateInput.setLayoutData(new GridData());
refDateInput.setValue(new Date());
calculateProductsButton1 = new Button(resultingProductsGroup, SWT.NONE);
setupImageButton(calculateProductsButton1, Images.getButtonRefresh());
calculateProductsButton1.setLayoutData(new GridData());
GridDataFactory.swtDefaults().hint(18, 18).applyTo(
calculateProductsButton1);
resultingProductsTable = new TableListWidget<Product>(
resultingProductsGroup, SWT.BORDER, ListWidgetMode.MULTI);
resultingProductsTable.setLinesVisible(true);
resultingProductsTable.setHeaderVisible(true);
final GridData rpTableProperty = new GridData(SWT.FILL, SWT.FILL, true,
true, 3, 1);
resultingProductsTable.setLayoutData(rpTableProperty);
GridDataFactory.swtDefaults().hint(230, 240).applyTo(
resultingProductsTable);
setupResultingProductsTableColumns();
resultingProductsTable.sortByComparator(new Comparator<Product>() {
#Override
public int compare(Product o1, Product o2) {
return o1.getPartNum().getExternalId().compareTo(
o2.getPartNum().getExternalId());
}
});
resultingProductsTable.addOpenListener(new IOpenListener() {
#Override
public void open(OpenEvent event) {
doResultingProductsTableOpen();
}
});
calculateProductsButton2 = new Button(resultingProductsGroup, SWT.NONE);
calculateProductsButton2.setText("Calculate");
whitelistAddButton = new Button(resultingProductsGroup, SWT.NONE);
whitelistAddButton.setText("Whitelist");
whitelistAddButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(final SelectionEvent e) {
doAddToWhitelist();
}
});
blacklistAddButton = new Button(resultingProductsGroup, SWT.NONE);
blacklistAddButton.setText("Blacklist");
blacklistAddButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(final SelectionEvent e) {
doAddToBlacklist();
}
});
What am I not seeing here? I'm stuck with this GUI bug for over 2 days now... Please, help me :)
You could design the whole composite with one GridLayout and 3 columns, while using horizontal span of 3 on the table. That doesn't give you the desired mocked up screen though, because reference date controls and buttons at the bottom would be aligned in columns.
Try instead using 3 composites
reference date: row layout
table: fill layout
button list: row layout

Java SWT: removeAll() in TABLE doesn't delete my buttons on the rows

I have a Table with 4 columns, and the fourth is filled with buttons with CHECK style.
The problem is that every time I do a removeAll() for fill the table with other content, the buttons are not being deleted, so I can see the same buttons with the same status in the same rows.
This is my 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("Payed");
And this is the way I'm populating the table, also you can see the buttons:
// populate Table
for (int i=0; i<50; i++) {
TableItem tableItem = new TableItem(membersTable, SWT.NONE);
tableItem.setText(new String[] {"person "+i, "610610620", "100"});
Button button = new Button(membersTable, SWT.CHECK);
button.pack();
TableEditor editor = new TableEditor(membersTable);
editor.minimumWidth = button.getSize().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(button, tableItem, 3);
button.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//how to know in which row is clicked the checkbox?
}
});
}
The TableEditor is not a TableItem and will not be removed by removeAll (neither by clearAll).
The corresponding controls can be disposed programmatically, like in:
membersTable.removeAll();
Arrays.stream(membersTable.getChildren()) // stream over all children
.filter(Button.class::isInstance) // only the Buttons
.forEach(Control::dispose); // and dispose it
or (same as above)
membersTable.removeAll();
for(Control control : membersTable.getChildren()) {
if (control instanceof Button) {
control.dispose();
}
}
Alternative, save the TableEditors in a list and dispose theirs editors:
List<TableEditor> editors = new ArrayList<>();
...
membersTable.removeAll();
editors.stream() // stream over TableEditors
.map(TableEditor::getEditor) // get their Editor (a Control)
.forEach(Control::dispose); // dispose it
editors.clear();

Discrete scrolling in SWT Table

How to do a discrete (row-by-row) scrolling in SWT Table (JFace TableViewer)?
I need a Table to be scrolled "one unbroken row at a time", putting a full cell on top.
I use JFace TableViewer, but I didn't find a way to add a mouse-listener to it, so I made something like this:
TableViewer table = new TableViewer(shell, SWT.BORDER_DASH |SWT.FULL_SELECTION);
//some visual settings ommited here
table.getControl().addMouseWheelListener(new MouseWheelListener() {
#Override
public void mouseScrolled(MouseEvent e) {
Table sourceControl = (Table)e.getSource();
System.out.println(e.count);
if(e.count >=0)
sourceControl.setTopIndex(sourceControl.getTopIndex()-1);
else
sourceControl.setTopIndex(sourceControl.getTopIndex()+1);
}
});
But it turned out, that first of all if e.count equals to 3 or more, some rows are being missed. Secondly sometimes setTopIndex() not placing rows correctly.
Can it be done in more accurate way?
From what I can tell, adding a e.doit = false in the Listener works perfectly. Here is an example:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.NONE);
for (int i = 0; i < 100; i++)
{
TableItem item = new TableItem(table, SWT.NONE);
item.setText("Item " + i);
}
table.addListener(SWT.MouseWheel, new Listener()
{
#Override
public void handleEvent(Event e)
{
e.doit = false;
if (e.count >= 0)
table.setTopIndex(table.getTopIndex() - 1);
else
table.setTopIndex(table.getTopIndex() + 1);
}
});
shell.pack();
shell.setSize(shell.getSize().x, 300);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
The only case where the TableItem at the top isn't displayed completely is when you reach the end of the Table and the table's height isn't an exact multiple of the TableItem's height.

How to change the foreground color of specific items in a List?

When I press a button, I want to change the foreground color of the selected item in a List.
So far, I tried this:
list.setForeground(display.getSystemColor(SWT.COLOR_RED));
but it changes the foreground color of all the items, not just the selected one.
Any ideas how to solve this?
Doing this with a List would require custom drawing. You are better off using a Table instead (or even a TableViewer depending on your requirements). Here is an example of a table that does what you want:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
shell.setText("StackOverflow");
final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
for (int i = 0; i < 10; i++)
{
TableItem item = new TableItem(table, SWT.NONE);
item.setText("item " + i);
}
Button button = new Button(shell, SWT.PUSH);
button.setText("Color selected");
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
List<TableItem> allItems = new ArrayList<>(Arrays.asList(table.getItems()));
TableItem[] selItems = table.getSelection();
for (TableItem item : selItems)
{
item.setForeground(display.getSystemColor(SWT.COLOR_RED));
allItems.remove(item);
}
for (TableItem item : allItems)
{
item.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Before button press:
After button press:
Just a note: This is not the most efficient way to do it, but should give you the basic idea.
List does not supports what you want.
Use Table and Table items instead.
Each table item represent a row, and it has setForeground(Color) method.

Categories

Resources