|
Re: How to add new data to ComboViewer [message #756134 is a reply to message #756062] |
Fri, 11 November 2011 07:32 |
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 #756181 is a reply to message #756062] |
Fri, 11 November 2011 12:08 |
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
|
|
|
Powered by
FUDForum. Page generated in 0.02234 seconds