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!");
}
}