0

I've got two lists within a dialog box and based on a selection of an item made in one, I'd like to remove an item in the second list. The problem is that, I can only access the selected items via getSelection() for BOTH lists on pressing the Ok! button. Calling the function to receive the selected elements from both lists anywhere before the selection listener of the Ok! button, does not work i.e, says that there is no array containing the selections to be returned. Can anyone please advise me on how I can achieve this functionality? Thanks.

  import org.eclipse.swt.widgets.Display;
  import org.eclipse.swt.widgets.Shell;
  import org.eclipse.swt.widgets.List;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.widgets.Button;
  import org.eclipse.swt.events.SelectionAdapter;
  import org.eclipse.swt.events.SelectionEvent;


 public class TwoLists {

protected Shell shell;

/**
 * Launch the application.
 * @param args
 */
public static void main(String[] args) {
    try {
        TwoLists window = new TwoLists();
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Open the window.
 */
public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

/**
 * Create contents of the window.
 */
protected void createContents() {
    shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");

    final List list = new List(shell, SWT.BORDER);
    list.setBounds(10, 24, 120, 144);
    list.add("Beijing");
    list.add("New York");
    list.add("Tahiti");

    final List list_1 = new List(shell, SWT.BORDER);
    list_1.setBounds(186, 24, 145, 144);
    list_1.add("Beijing");
    list_1.add("New York");
    list_1.add("Tahiti");


    /*int index= list_1.getSelectionIndex();
    System.out.println(index);          THROWS AN INDEX OUT OF BOUNDS
    list.remove(index);*/

    Button btnOk = new Button(shell, SWT.NONE);
    btnOk.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            String[] selectedDestination= list.getSelection();
            String[] selectedOrigin= list_1.getSelection();
            System.out.println(selectedOrigin[0]+" to "+selectedDestination[0]);
        }
    });
    btnOk.setBounds(99, 227, 75, 25);
    btnOk.setText("OK!");

}
}
Asher
  • 811
  • 3
  • 10
  • 19

1 Answers1

1

What you're doing doesn't work, because you're requesting the selected item when none has been selected yet.

What you need to do instead is add a Listener to both Lists for SWT.Selection and modify the other list according to the selection.


However, what you want to do seems to be very similar to an answer I posted to this question.


A final remark: Please use Layouts instead of setting the bounds of widgets directly. It might look ok on the screen resolution you're developing on, but may look completely out of place on a different resolution...

Community
  • 1
  • 1
Baz
  • 36,440
  • 11
  • 68
  • 94
  • Sorry for not getting back to you earlier. I'm trying to work out what you advised with the Listeners currently. The linked question didn't really help me. I left out the Layout in the example snippet but I will definitely keep it in mind for applications I work again. Thanks again for the answer! – Asher Aug 04 '14 at 10:18
  • @Asher Ok, when you're done evaluating, please consider accepting and upvoting my answer if it was helpful, otherwise, please clarify :) – Baz Aug 04 '14 at 10:27