Search code examples
jfacercp

Cannot get the selected item in a ListViewer in a JFace Dialog


I created a dialog class that inherited from JFace Dialog using Windows Builder. In that, I added some controls included a button and a JFace ListViewer. In widgetSelected() function of the button, I can get out the selected item in the ListViewer. But in `okPressed(), I cannot get this. I don't know why. Can you help me?

Thanks!


Solution

  • If you want to access UI elements in okPressed you must do so before calling super.okPressed() because that will close the dialog and dispose of the controls. So something like:

    @Override
    protected void okPressed()
    {
      IStructuredSelection sel = viewer.getStructuredSelection();
    
      // TODO deal with selection
    
      // Call super.okPressed() last
      super.okPressed();
    }
    

    Alternatively save the selection when your widgetSelected is called.