Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » table drawing question....
table drawing question.... [message #448176] Tue, 04 January 2005 22:26 Go to next message
Eclipse UserFriend
Originally posted by: gopowereclipse.yahoo.com

I have a table where one of the columns is customized. The column contains
the colored and bounded labels as its TableEditors. I'd like to update the
label with different foreground and background color when the row is
selected and de-selected. I registered a SelectionListener on the table. The
SelectionListener alters the foreground and the background color of the
label. When a row is selected, the SelectionListener is notified after the
table is redrawn. I observed the latency between the selection highlight
moving to a different row and the label color changing. It's quite annoying.
You can see the label selection jumpping around. Does anyone know how to
make the drawing smooth in my situation? Thanks a lot for your help.

--Vivian
Re: table drawing question.... [message #448194 is a reply to message #448176] Wed, 05 January 2005 14:57 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Post a small stand alone snippet that shows the problem.

"Vivian" <gopowereclipse@yahoo.com> wrote in message
news:crf565$k28$1@www.eclipse.org...
> I have a table where one of the columns is customized. The column contains
> the colored and bounded labels as its TableEditors. I'd like to update the
> label with different foreground and background color when the row is
> selected and de-selected. I registered a SelectionListener on the table.
The
> SelectionListener alters the foreground and the background color of the
> label. When a row is selected, the SelectionListener is notified after the
> table is redrawn. I observed the latency between the selection highlight
> moving to a different row and the label color changing. It's quite
annoying.
> You can see the label selection jumpping around. Does anyone know how to
> make the drawing smooth in my situation? Thanks a lot for your help.
>
> --Vivian
>
>
Re: table drawing question.... [message #448538 is a reply to message #448194] Tue, 11 January 2005 19:42 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gopowereclipse.yahoo.com

Here is the code snippet. The latency is subtle in this snippet. In my real
code, the table is more complicated. It takes longer to get to
updateSelection() method. The delay is observed more frequently. I added a
for loop in method updateSelection() to exaggerate the delay. Thanks!

--Vivian

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ColumnLayoutData;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class SelectionSnippet
{
protected Table collectionTable;
protected List sequenceList = new ArrayList();
protected List disposableList = new ArrayList ();
protected int selectionIndex = -1;

static final protected int NUM_COLUMNS = 2;
static final protected ColumnLayoutData[] columnLayouts = {
new ColumnPixelData(30),
new ColumnWeightData(100) };
static final protected int[] columnAlignments = {
SWT.LEFT,
SWT.LEFT };
static final protected String[][] items;
static {
items = new String[30][];
for (int i = 0; i < 30; i++)
{
items[i] = new String[2];
items[i][0] = new Integer(i).toString();
items[i][1] = "Text " + i;
}
};

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);

shell.setLayout(new RowLayout());

SelectionSnippet s = new SelectionSnippet();
s.createCollectionTable (shell);
s.populateCollectionTable ();

shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

private void createCollectionTable(final Composite parent)
{
this.collectionTable =
new Table(parent,
SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
this.collectionTable.setLayoutData(new RowData(150, 300));

TableLayout tableLayout = new TableLayout();
this.collectionTable.setLayout(tableLayout);
for (int i = 0; i < NUM_COLUMNS; i++) {
tableLayout.addColumnData(columnLayouts[i]);
TableColumn column = new TableColumn(this.collectionTable,
columnAlignments[i]);
}

this.collectionTable.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent e) {
updateSelection();
refresh();
}

public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
}

});

this.collectionTable.addFocusListener(new FocusListener() {

public void focusGained(FocusEvent e) {
updateSelection();
}

public void focusLost(FocusEvent e) {
updateSelection();
}
});
}


protected void populateCollectionTable()
{
// populate the collection table
for (int i = 0; i < items.length; i++) {
addRow();
}

this.selectionIndex = 0;
refresh();
}

protected void addRow()
{
TableItem item = new TableItem(collectionTable, SWT.NONE);

// set a TableEditor for column 1 to display the Sequence Number
Label label = new Label(collectionTable, SWT.BORDER | SWT.CENTER
| SWT.SHADOW_IN);
label.setBackground(label.getDisplay().getSystemColor(
SWT.COLOR_WIDGET_DARK_SHADOW));

TableEditor editor = new TableEditor(collectionTable);
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.horizontalAlignment = SWT.RIGHT;
editor.minimumWidth = 30;
editor.minimumHeight = collectionTable.getItemHeight();
editor.setEditor(label, item, 0);

sequenceList.add(label);
disposableList.add(editor);
}

protected void updateSelection()
{
// added to exaggerate the delay
for (int i = 0; i < 100000000; i++){}

//update the old selection
if (selectionIndex >= 0 && !sequenceList.isEmpty()) {
Label sequenceLabel = (Label) sequenceList.get(selectionIndex);
sequenceLabel.setForeground(sequenceLabel.getDisplay()
.getSystemColor(SWT.COLOR_BLACK));
sequenceLabel.setBackground(sequenceLabel.getDisplay()
.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
}

//update the new selection
selectionIndex = this.collectionTable.getSelectionIndex();
if (selectionIndex > -1) {
Label sequenceLabel = (Label) sequenceList.get(selectionIndex);
if (this.collectionTable.isFocusControl()) {
sequenceLabel.setForeground(sequenceLabel.getDisplay()
.getSystemColor(SWT.COLOR_WHITE));
sequenceLabel.setBackground(sequenceLabel.getDisplay()
.getSystemColor(SWT.COLOR_LIST_SELECTION));
} else {
sequenceLabel.setBackground(sequenceLabel.getDisplay()
.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
}
}
}

protected void refresh()
{
// populate the collection table with the content
TableItem[] tableItems = collectionTable.getItems();
for (int i = 0; tableItems != null && i < tableItems.length; i++)
{
TableItem tableItem = tableItems[i];

// Column 1: Sequence Number
Label label = (Label) sequenceList.get(i);
label.setText(items[i][0]);

// Column 2: Name
tableItem.setText(1, items[i][1]);
}

// redraw the underlying widgets
for (int i = 0; i < disposableList.size(); i++)
{
TableEditor editor = (TableEditor) disposableList.get(i);
editor.layout();
}
this.collectionTable.redraw();

// update the selection
this.collectionTable.setSelection(selectionIndex);
updateSelection();
}
}
"Steve Northover" <steve_northover@ca.ibm.com> wrote in message
news:crgva1$6ia$1@www.eclipse.org...
> Post a small stand alone snippet that shows the problem.
>
> "Vivian" <gopowereclipse@yahoo.com> wrote in message
> news:crf565$k28$1@www.eclipse.org...
> > I have a table where one of the columns is customized. The column
contains
> > the colored and bounded labels as its TableEditors. I'd like to update
the
> > label with different foreground and background color when the row is
> > selected and de-selected. I registered a SelectionListener on the table.
> The
> > SelectionListener alters the foreground and the background color of the
> > label. When a row is selected, the SelectionListener is notified after
the
> > table is redrawn. I observed the latency between the selection highlight
> > moving to a different row and the label color changing. It's quite
> annoying.
> > You can see the label selection jumpping around. Does anyone know how to
> > make the drawing smooth in my situation? Thanks a lot for your help.
> >
> > --Vivian
> >
> >
>
>
Re: table drawing question.... [message #448635 is a reply to message #448538] Wed, 12 January 2005 16:32 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
This is a bug/feature is in Windows. When you select an item, Windows can't
tell whether you are starting a drag and drop operating or selecting. If
you release the mouse button or move the mouse slightly, then it figures out
you are selecting and sends the notification. The problem is that it
doesn't send the SWT.Selection event until after all this happens which
means that your code doesn't run until later. See:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=26605


"Vivian" <gopowereclipse@yahoo.com> wrote in message
news:cs1a7e$iqh$1@www.eclipse.org...
> Here is the code snippet. The latency is subtle in this snippet. In my
real
> code, the table is more complicated. It takes longer to get to
> updateSelection() method. The delay is observed more frequently. I added a
> for loop in method updateSelection() to exaggerate the delay. Thanks!
>
> --Vivian
>
> import java.util.ArrayList;
> import java.util.List;
>
> import org.eclipse.jface.viewers.ColumnLayoutData;
> import org.eclipse.jface.viewers.ColumnPixelData;
> import org.eclipse.jface.viewers.ColumnWeightData;
> import org.eclipse.jface.viewers.TableLayout;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.custom.TableEditor;
> import org.eclipse.swt.events.FocusEvent;
> import org.eclipse.swt.events.FocusListener;
> import org.eclipse.swt.events.SelectionEvent;
> import org.eclipse.swt.events.SelectionListener;
> import org.eclipse.swt.layout.RowData;
> import org.eclipse.swt.layout.RowLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Table;
> import org.eclipse.swt.widgets.TableColumn;
> import org.eclipse.swt.widgets.TableItem;
>
> public class SelectionSnippet
> {
> protected Table collectionTable;
> protected List sequenceList = new ArrayList();
> protected List disposableList = new ArrayList ();
> protected int selectionIndex = -1;
>
> static final protected int NUM_COLUMNS = 2;
> static final protected ColumnLayoutData[] columnLayouts = {
> new ColumnPixelData(30),
> new ColumnWeightData(100) };
> static final protected int[] columnAlignments = {
> SWT.LEFT,
> SWT.LEFT };
> static final protected String[][] items;
> static {
> items = new String[30][];
> for (int i = 0; i < 30; i++)
> {
> items[i] = new String[2];
> items[i][0] = new Integer(i).toString();
> items[i][1] = "Text " + i;
> }
> };
>
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
>
> shell.setLayout(new RowLayout());
>
> SelectionSnippet s = new SelectionSnippet();
> s.createCollectionTable (shell);
> s.populateCollectionTable ();
>
> shell.pack();
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
>
> private void createCollectionTable(final Composite parent)
> {
> this.collectionTable =
> new Table(parent,
> SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
> this.collectionTable.setLayoutData(new RowData(150, 300));
>
> TableLayout tableLayout = new TableLayout();
> this.collectionTable.setLayout(tableLayout);
> for (int i = 0; i < NUM_COLUMNS; i++) {
> tableLayout.addColumnData(columnLayouts[i]);
> TableColumn column = new TableColumn(this.collectionTable,
> columnAlignments[i]);
> }
>
> this.collectionTable.addSelectionListener(new SelectionListener() {
>
> public void widgetSelected(SelectionEvent e) {
> updateSelection();
> refresh();
> }
>
> public void widgetDefaultSelected(SelectionEvent e) {
> widgetSelected(e);
> }
>
> });
>
> this.collectionTable.addFocusListener(new FocusListener() {
>
> public void focusGained(FocusEvent e) {
> updateSelection();
> }
>
> public void focusLost(FocusEvent e) {
> updateSelection();
> }
> });
> }
>
>
> protected void populateCollectionTable()
> {
> // populate the collection table
> for (int i = 0; i < items.length; i++) {
> addRow();
> }
>
> this.selectionIndex = 0;
> refresh();
> }
>
> protected void addRow()
> {
> TableItem item = new TableItem(collectionTable, SWT.NONE);
>
> // set a TableEditor for column 1 to display the Sequence Number
> Label label = new Label(collectionTable, SWT.BORDER | SWT.CENTER
> | SWT.SHADOW_IN);
> label.setBackground(label.getDisplay().getSystemColor(
> SWT.COLOR_WIDGET_DARK_SHADOW));
>
> TableEditor editor = new TableEditor(collectionTable);
> editor.grabHorizontal = true;
> editor.grabVertical = true;
> editor.horizontalAlignment = SWT.RIGHT;
> editor.minimumWidth = 30;
> editor.minimumHeight = collectionTable.getItemHeight();
> editor.setEditor(label, item, 0);
>
> sequenceList.add(label);
> disposableList.add(editor);
> }
>
> protected void updateSelection()
> {
> // added to exaggerate the delay
> for (int i = 0; i < 100000000; i++){}
>
> //update the old selection
> if (selectionIndex >= 0 && !sequenceList.isEmpty()) {
> Label sequenceLabel = (Label) sequenceList.get(selectionIndex);
> sequenceLabel.setForeground(sequenceLabel.getDisplay()
> .getSystemColor(SWT.COLOR_BLACK));
> sequenceLabel.setBackground(sequenceLabel.getDisplay()
> .getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
> }
>
> //update the new selection
> selectionIndex = this.collectionTable.getSelectionIndex();
> if (selectionIndex > -1) {
> Label sequenceLabel = (Label) sequenceList.get(selectionIndex);
> if (this.collectionTable.isFocusControl()) {
> sequenceLabel.setForeground(sequenceLabel.getDisplay()
> .getSystemColor(SWT.COLOR_WHITE));
> sequenceLabel.setBackground(sequenceLabel.getDisplay()
> .getSystemColor(SWT.COLOR_LIST_SELECTION));
> } else {
> sequenceLabel.setBackground(sequenceLabel.getDisplay()
> .getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
> }
> }
> }
>
> protected void refresh()
> {
> // populate the collection table with the content
> TableItem[] tableItems = collectionTable.getItems();
> for (int i = 0; tableItems != null && i < tableItems.length; i++)
> {
> TableItem tableItem = tableItems[i];
>
> // Column 1: Sequence Number
> Label label = (Label) sequenceList.get(i);
> label.setText(items[i][0]);
>
> // Column 2: Name
> tableItem.setText(1, items[i][1]);
> }
>
> // redraw the underlying widgets
> for (int i = 0; i < disposableList.size(); i++)
> {
> TableEditor editor = (TableEditor) disposableList.get(i);
> editor.layout();
> }
> this.collectionTable.redraw();
>
> // update the selection
> this.collectionTable.setSelection(selectionIndex);
> updateSelection();
> }
> }
> "Steve Northover" <steve_northover@ca.ibm.com> wrote in message
> news:crgva1$6ia$1@www.eclipse.org...
> > Post a small stand alone snippet that shows the problem.
> >
> > "Vivian" <gopowereclipse@yahoo.com> wrote in message
> > news:crf565$k28$1@www.eclipse.org...
> > > I have a table where one of the columns is customized. The column
> contains
> > > the colored and bounded labels as its TableEditors. I'd like to update
> the
> > > label with different foreground and background color when the row is
> > > selected and de-selected. I registered a SelectionListener on the
table.
> > The
> > > SelectionListener alters the foreground and the background color of
the
> > > label. When a row is selected, the SelectionListener is notified after
> the
> > > table is redrawn. I observed the latency between the selection
highlight
> > > moving to a different row and the label color changing. It's quite
> > annoying.
> > > You can see the label selection jumpping around. Does anyone know how
to
> > > make the drawing smooth in my situation? Thanks a lot for your help.
> > >
> > > --Vivian
> > >
> > >
> >
> >
>
>
Previous Topic:TablkeCursor fails to update when model changes
Next Topic:is Label with transparent PNG possible?
Goto Forum:
  


Current Time: Thu Apr 25 18:07:26 GMT 2024

Powered by FUDForum. Page generated in 0.02937 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top