Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How select an item in a Table ?
How select an item in a Table ? [message #461731] Wed, 28 September 2005 20:11 Go to next message
NiS is currently offline NiSFriend
Messages: 58
Registered: July 2009
Member
I want the first element to be selected in a viewer.

I tried:

public void createPartControl(Composite parent) {

fViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);

....

final Table table = fViewer.getTable();

int selectionCount = table.getSelectionCount();

if(selectionCount==0) {

table.setSelection(0);

}





Nothing happens: any item is selected in my view.



How can I do this?



Nicolas.
Re: How select an item in a Table ? [message #461733 is a reply to message #461731] Wed, 28 September 2005 21:07 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"Nicolas Silb" <NiSJunk@DigitalAirways.com> wrote in message
news:dhetcc$8b0$1@news.eclipse.org...
>I want the first element to be selected in a viewer.
>
> I tried:
>
> public void createPartControl(Composite parent) {
>
> fViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL |
> SWT.V_SCROLL);
>
> ...
>
> final Table table = fViewer.getTable();
>
> int selectionCount = table.getSelectionCount();
>
> if(selectionCount==0) {
>
> table.setSelection(0);
>
> }
>
>
fViewer.setSelection(new StructuredSelection(obj));

where obj is the first element in your viewer input.
---
Sunil
Re: How select an item in a Table ? [message #461735 is a reply to message #461733] Wed, 28 September 2005 22:14 Go to previous messageGo to next message
NiS is currently offline NiSFriend
Messages: 58
Registered: July 2009
Member
Where should i put this? if i put it after creating the viewer, there are no
content yet and it fails;

I tried:

public void createPartControl(Composite parent) {

fViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);

fViewer.setContentProvider(new ViewContentProvider());

fViewer.setLabelProvider(new ViewLabelProvider());

fViewer.setSorter(new ViewerSorter());


addSelectionChangedListener(Repository.getInstance());


ISelection selection = new
StructuredSelection(fViewer.getTable().getItem(0));

fViewer.setSelection(selection, true);





"Sunil Kamath" <sunil_kamath@nohotspammail.com> a
Re: How select an item in a Table ? [message #461737 is a reply to message #461735] Thu, 29 September 2005 04:19 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"Nicolas Silb" <NiSJunk@DigitalAirways.com> wrote in message
news:dhf4k8$ho5$1@news.eclipse.org...
> Where should i put this? if i put it after creating the viewer, there are
> no content yet and it fails;
>

Obviously. If there is no content, there is no way to select an item.

> I tried:
>
> public void createPartControl(Composite parent) {
>
> fViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL |
> SWT.V_SCROLL);
>
> fViewer.setContentProvider(new ViewContentProvider());
>
> fViewer.setLabelProvider(new ViewLabelProvider());
>
> fViewer.setSorter(new ViewerSorter());
>
>
> addSelectionChangedListener(Repository.getInstance());
>

At which point are you calling fViewer.setInput()?

>
> ISelection selection = new
> StructuredSelection(fViewer.getTable().getItem(0));
>
> fViewer.setSelection(selection, true);
>

This won't work. It expects the contents of the StructuredSelection to be an
element from your model- not the table item itself.
What is the input for your TableViewer?
---
Sunil
Re: How select an item in a Table ? [message #461748 is a reply to message #461737] Thu, 29 September 2005 09:58 Go to previous message
NiS is currently offline NiSFriend
Messages: 58
Registered: July 2009
Member
GOT IT!

The trick is to do it AFTER setting the setInput (thanks for your "At which
point are you calling fViewer.setInput()?" question)

Here is the working code:

public void createPartControl(Composite parent) {

fViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);

fViewer.setContentProvider(new ViewContentProvider());

fViewer.setLabelProvider(new ViewLabelProvider());

fViewer.setSorter(new ViewerSorter());


fViewer.setInput(this/*dummy*/);

Object item = fViewer.getElementAt(0);

ISelection selection = new StructuredSelection(item);

fViewer.setSelection(selection, true);


merci +++

Nicolas.


"Sunil Kamath" <sunil_kamath@nohotspammail.com> a
Previous Topic:Splash screen and own dialog window
Next Topic:Tree with columns - still not solved
Goto Forum:
  


Current Time: Wed Sep 23 09:31:39 GMT 2020

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

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

Back to the top