Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » setSelection in TableViewer unable to select row from non-UI thread.
setSelection in TableViewer unable to select row from non-UI thread. [message #528826] Wed, 21 April 2010 23:48 Go to next message
PM  is currently offline PM Friend
Messages: 14
Registered: July 2009
Junior Member
Hi,

I have trying to select/highlight a row in JFace TableViewer from a non-ui thread using setSelection method. Somehow i am not able to get it working. I am using setSelection method. Somehow my viewer gets completely messed up. I am able to see all the rows above the "selected" row. I dont see anything below the "selected" row and only 1st column of the selected row is visible.
I want to retain "selection" of a row after some updates have taken place. TableViewer is VIRTUAL.
Not sure what i am doing wrong. Any suggestions/pointers will be a great help.
Here is a snippet, i can provide more details.

private void subscribeToJMS()
{
Runnable r = new Runnable () {
public void run() {
display.syncExec (new Runnable () {
public void run () {
if( display != null && !display.isDisposed() )
updateViewer();
}
});
}
};

public synchronized void updateViewer()
{
int index = model.indexOf(ot);
if(index != -1) {
model.remove(ot);
if (brrOrder.getTopicName().equals(IBrrConstants.ETS_BRR_FOR_LO CK) ||
brrOrder.getTopicName().equals(IBrrConstants.ETS_BRR_FOR_UNL OCK)){
model.add(index, ot);
}
grid.updateModel(model, false, true); // does a tableViewer.refresh
// sets the selection back.
StructuredSelection sel = new StructuredSelection(tableViewer.getElementAt(index));
tableViewer.setSelection(sel, true);
}
}

Thanx
Priyanka

[Updated on: Wed, 21 April 2010 23:57]

Report message to a moderator

Re: setSelection in TableViewer unable to select row from non-UI thread. [message #528841 is a reply to message #528826] Thu, 22 April 2010 06:16 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 22.04.2010 01:48, PM wrote:
> Hi,
>
> I have trying to select/highlight a row in JFace TableViewer from a
> non-ui thread using setSelection method. Somehow i am not able to get it
> working. I am using setSelection method. Somehow my viewer gets
> completely messed up. I am able to see all the rows above the "selected"
> row. I dont see anything below the "selected" row and only 1st column of
> the selected row is visible. I want to retain "selection" of a row after
> some updates have taken place.
> Not sure what i am doing wrong. Any suggestions/pointers will be a great
> help.
> Here is a snippet, i can provide more details.
>
> private void subscribeToJMS()
> {
> Runnable r = new Runnable () {
> public void run() {
> display.syncExec (new Runnable () {
> public void run () {
> if( display != null && !display.isDisposed() )
> updateViewer();
> }
> });
> }
> };

It does not make much sense to combine syncExec with a query for
display != null && !display.isDisposed(). To prevent a potential
deadlock problem, use asyncExec *with* above test.

> public synchronized void updateViewer()
> {
> int index = model.indexOf(ot);
> if(index != -1) {
> model.remove(ot);
> if (brrOrder.getTopicName().equals(IBrrConstants.ETS_BRR_FOR_LO CK) ||
> brrOrder.getTopicName().equals(IBrrConstants.ETS_BRR_FOR_UNL OCK)){
> model.add(index, ot);
> }
> grid.updateModel(model, false, true);
> // sets the selection back.
> StructuredSelection sel = new
> StructuredSelection(tableViewer.getElementAt(index));
> tableViewer.setSelection(sel, true);
> }
> }

This looks more like a jface problem (based on TableViewer
API) than a pure SWT problem. Some hints:

1) Don't use TableViewer.getElementAt, this is no official
API ("This method is internal to the framework").

2) You don't inform the Table(Viewer) about your changes
(I do only see that you change your model). Based on jface
API you should call Viewer.refresh();

There could still be problems with your code, but that
is hard to diagnose without a concrete simple program
snippet that demonstrate the unexpected behaviour.

HTH & Greetings from Bremen,

Daniel Krügler
Re: setSelection in TableViewer unable to select row from non-UI thread. [message #528844 is a reply to message #528841] Thu, 22 April 2010 06:24 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6494
Registered: July 2009
Senior Member
Am 22.04.10 08:16, schrieb Daniel Krügler:
> On 22.04.2010 01:48, PM wrote:
>> Hi,
>>
>> I have trying to select/highlight a row in JFace TableViewer from a
>> non-ui thread using setSelection method. Somehow i am not able to get it
>> working. I am using setSelection method. Somehow my viewer gets
>> completely messed up. I am able to see all the rows above the "selected"
>> row. I dont see anything below the "selected" row and only 1st column of
>> the selected row is visible. I want to retain "selection" of a row after
>> some updates have taken place.
>> Not sure what i am doing wrong. Any suggestions/pointers will be a great
>> help.
>> Here is a snippet, i can provide more details.
>>
>> private void subscribeToJMS()
>> {
>> Runnable r = new Runnable () {
>> public void run() {
>> display.syncExec (new Runnable () {
>> public void run () {
>> if( display != null && !display.isDisposed() )
>> updateViewer();
>> }
>> });
>> }
>> };
>
> It does not make much sense to combine syncExec with a query for
> display != null && !display.isDisposed(). To prevent a potential
> deadlock problem, use asyncExec *with* above test.
>
>> public synchronized void updateViewer()
>> {
>> int index = model.indexOf(ot);
>> if(index != -1) {
>> model.remove(ot);
>> if (brrOrder.getTopicName().equals(IBrrConstants.ETS_BRR_FOR_LO CK) ||
>> brrOrder.getTopicName().equals(IBrrConstants.ETS_BRR_FOR_UNL OCK)){
>> model.add(index, ot);
>> }
>> grid.updateModel(model, false, true);
>> // sets the selection back.
>> StructuredSelection sel = new
>> StructuredSelection(tableViewer.getElementAt(index));
>> tableViewer.setSelection(sel, true);
>> }
>> }
>

What is grid.updateModel() doing? Where is ot coming from? Why are you
not calling new StructuredSelection(ot)? It looks like there's a check
missing which tests for -1 (the remove case)?

> This looks more like a jface problem (based on TableViewer
> API) than a pure SWT problem. Some hints:
>
> 1) Don't use TableViewer.getElementAt, this is no official
> API ("This method is internal to the framework").
>
> 2) You don't inform the Table(Viewer) about your changes
> (I do only see that you change your model). Based on jface
> API you should call Viewer.refresh();
>

or Viewer#remove/add.

Tom
Re: setSelection in TableViewer unable to select row from non-UI thread. [message #529061 is a reply to message #528844] Thu, 22 April 2010 19:12 Go to previous message
PM  is currently offline PM Friend
Messages: 14
Registered: July 2009
Junior Member
Thanx very much Daniel & Tom. I was able to solve my problem using your inputs.
I did 3 things that seemed to work.

1. Swapped syncExec with asyncExec.
2. Performed tableViewer.refresh() after underlying model is updated.
3. Replaced StructuredSelection sel = new StructuredSelection(tableViewer.getElementAt(index) by StructuredSelection sel = new StructuredSelection(ot);

public synchronized void updateViewer()
{
List<ITopsModelObject> model = grid.getModel();

// check for updates
// If the OrderTicket is already in the model, remove and then add.
for (Iterator<ITopsModelObject> it = newList.iterator(); it.hasNext();){
ITopsModelObject ot = it.next();

// should use overridden equals and hashCode method.
int index = model.indexOf(ot);
if(index != -1) {
model.remove(ot);
if (brrOrder.getTopicName().equals(IBrrConstants.ETS_BRR_FOR_LO CK) ||
brrOrder.getTopicName().equals(IBrrConstants.ETS_BRR_FOR_UNL OCK)){
model.add(index, ot);
}
tableViewer.refresh();
// sets the selection back.
StructuredSelection sel = new StructuredSelection(ot);
tableViewer.setSelection(sel, true);
}
}
}

Thanx once again for your time and your prompt replies.
Previous Topic:Whats wrong with the following tiny code using TreeEditor
Next Topic:zoom ScalableFreeformRootEditPart using a Zoom Bar
Goto Forum:
  


Current Time: Sat Aug 17 17:09:18 GMT 2019

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

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

Back to the top