Santhosh
Santhosh

Reputation: 1

Windows 7 SWT COMBO Issue

I am facing an issue with the SWT Combo in my eclipse RCP application.

I will try explaining my issue with a use case for better understanding.

  1. I have a combo box in a Eclipse RCP View with values say "A", "B","C","D" and i have a submit button beside it & a SWT table right below it.

  2. Once the value is changed in the Combo and submit button is clicked, records would be displayed in the table.

  3. Let us suppose "A" is selected by default and records of A are displayed in the table on view invocation.

  4. Now I select "B" from the drop down and click submit. I see only the records of "A" in the table although the combo shows "B".

  5. ONLY if I select "B" again from the combo and then click submit, "B"'s records gets displayed.

  6. Now if I select C from the combo , only "B"'s records gets displayed.

  7. Later, If i select D from the Combo , "C"'s records are displayed.

It seems that only the previous selections is processed and displayed rather than the current selection.

I am not facing this issue in Windows XP or prev versions of windows. I recently shifted to Windows 7 64-bit OS where I faced this issue.

Is this a known issue? Any help would be appreciated.

Upvotes: 0

Views: 1173

Answers (2)

Santhosh
Santhosh

Reputation: 1


Please find the sample code below

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class TestCombo {


    private static String[] filterByText = new String[] {"A","B","C","D"};
    static int index = 0;

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


        Composite comp =  new Composite(shell, SWT.NONE);

        GridLayout layout = new  GridLayout(2, false);
        GridData gridData = new GridData(SWT.FILL,SWT.FILL,true,false);

        comp.setLayout(layout);
        comp.setLayoutData(gridData);

        final Combo filter = new Combo (comp, SWT.READ_ONLY);
        filter.setItems (filterByText );
        filter.setText (filterByText[0]);
        filter.setVisibleItemCount( filterByText.length );

        filter.addListener(SWT.DROP_DOWN, new Listener() {

            @Override
            public void handleEvent(Event event) {
                index = filter.getSelectionIndex();

            }
        });

        Button submit = new Button (comp, SWT.PUSH);
        submit.setText ("Submit");
        GridData data = new GridData();
        data.widthHint = 80;
        submit.setLayoutData(data);
        submit.addSelectionListener (new SelectionAdapter () {
            public void widgetSelected(SelectionEvent e) {

                System.out.println("The index is ==> "+index);
            }
        });
        comp.pack();
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

}

Upvotes: 0

Claimos
Claimos

Reputation: 206

You use a drop down listener for storing the selected index. The drop down listener is triggered when the combo list drops down. At this time, the old selection is stored. If you select a new item in the list, the listener will not be triggered again. Though you always get the previous selected item index if you press the submit button later on.

To get what you want, you must use a selection listener instead of the drop down listener and all works fine. The selection listener is called when you select an item in the drop down list. Just replace SWT.DropDown with SWT.Selection.

filter.addListener(SWT.Selection, new Listener() {...});

Upvotes: 2

Related Questions