Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » How to add new data to ComboViewer
icon5.gif  How to add new data to ComboViewer [message #756062] Thu, 10 November 2011 17:40 Go to next message
Mikael Petterson is currently offline Mikael Petterson
Messages: 152
Registered: July 2009
Senior Member
Hi,

I have been googling and trying to find examples for how to add/update the list in ComboViewer. Most examples show the following:

ComboViewer comboViewer = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
comboViewer.setLabelProvider(new ActivityListLabelProvider());
comboViewer.setContentProvider(new ArrayContentProvider());
comboViewer.setInput(activities);

activities is an String [].

This works fine as log as my activities are static but I my case I have a method

String [] activities = getAllActivities();

Example:

First time it is executed:

e.g. activities will contain {"one","two","three"};

Second time it is executed ( new has been created):

e.g. activities will contain {"one","two","three","four"};

So it also contains the old ones.

My question is quite simple. How do I reflect this in my ComboViewer?

I will be very greatful for all input and help since I have not been able to solve it.

br,

//mike
Re: How to add new data to ComboViewer [message #756134 is a reply to message #756062] Fri, 11 November 2011 07:32 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel Krügler
Messages: 853
Registered: July 2009
Senior Member
On 2011-11-10 18:40, Mikael Petterson wrote:
> Hi,
>
> I have been googling and trying to find examples for how to add/update
> the list in ComboViewer. Most examples show the following:
>
> ComboViewer comboViewer = new ComboViewer(composite, SWT.DROP_DOWN |
> SWT.READ_ONLY);
> comboViewer.setLabelProvider(new ActivityListLabelProvider());
> comboViewer.setContentProvider(new ArrayContentProvider());
> comboViewer.setInput(activities);
>
> activities is an String [].
>
> This works fine as log as my activities are static but I my case I have
> a method
>
> String [] activities = getAllActivities();
>
> Example:
>
> First time it is executed:
>
> e.g. activities will contain {"one","two","three"};
>
> Second time it is executed ( new has been created):
>
> e.g. activities will contain {"one","two","three","four"};
>
> So it also contains the old ones.
>
> My question is quite simple. How do I reflect this in my ComboViewer?
>
> I will be very greatful for all input and help since I have not been
> able to solve it.

I'm not quite sure whether I understood you correctly. Why shouldn't you
simply call

comboViewer.setInput(getAllActivities());

as often as you wish? Each call will replace the current input by the
new one.

Alternatively you could define your own IStructuredContentProvider,
where the function getElements() calls getAllActivities() directly.

HTH & Greetings from Bremen,

- Daniel Krügler
Re: How to add new data to ComboViewer [message #756136 is a reply to message #756134] Fri, 11 November 2011 08:07 Go to previous messageGo to next message
Mikael Petterson is currently offline Mikael Petterson
Messages: 152
Registered: July 2009
Senior Member
Hi,

Thanks for reply.

When I used:

comboViewer.setInput(getAllActivities());

I got duplicates of the Strings.

So I decided to use the following solution:

First time ComboViewer is created I use:

comboViewer.setInput(getAllActivities());

Next time when I have added an activity I

Use the following:

activities = provider.listMyActivities();
// Select last added in arraylist
comboViewer.getCombo().removeAll();
comboViewer.add(getAllActivities());
comboViewer.setSelection(new StructuredSelection(activities.size() - 1), true);
comboViewer.refresh();

br,

//mike
Re: How to add new data to ComboViewer [message #756181 is a reply to message #756062] Fri, 11 November 2011 12:08 Go to previous message
Daniel Krügler is currently offline Daniel Krügler
Messages: 853
Registered: July 2009
Senior Member
On 2011-11-10 18:40, Mikael Petterson wrote:
> This works fine as log as my activities are static but I my case I have
> a method
>
> String [] activities = getAllActivities();
>
> Example:
>
> First time it is executed:
>
> e.g. activities will contain {"one","two","three"};
>
> Second time it is executed ( new has been created):
>
> e.g. activities will contain {"one","two","three","four"};
>
> So it also contains the old ones.

As described elsewhere you are not really explaining/demonstrating what
is wrong. Below is a fullfledged Java program that works for me. Once
you start it, the combo contains the expected three elements. After you
pressed the button on the right side you should see only a single
element in the combo.

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ComboViewerSnippet {

public ComboViewerSnippet() {
}

static String[] getAllActivities(boolean b) {
if (b) {
return new String[] { "one", "two", "three" };
} else {
return new String[] { "four" };
}
}

private static ComboViewer createViewer(Shell shell) {
final ComboViewer v = new ComboViewer(shell, SWT.DROP_DOWN
| SWT.READ_ONLY);
v.setLabelProvider(new LabelProvider());
v.setContentProvider(new ArrayContentProvider());
v.setInput(getAllActivities(true));
return v;
}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final ComboViewer v = createViewer(shell);
Button b = new Button(shell, SWT.PUSH);
b.setText("Set one element");
b.addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {
v.setInput(getAllActivities(false));
}

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

}

HTH & Greetings from Bremen,

Daniel Krügler
Previous Topic:ComboViewer dropdown list not updated with new data
Next Topic:How to have Tree View inside a Table
Goto Forum:
  


Current Time: Tue Sep 30 22:05:16 GMT 2014

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

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