简体   繁体   中英

Close JFace window on initialization

I have a JFace application with a list of files. A child window is opened when a user clicked on a file in the list. The child window automatically loads the file content into a Text widget.

Loading of the file content may fail. I can detect such a situation and I would be happy to close the child window before it is shown on the screen.

I tried to call close() at the end of overriden protected Control createContents(Composite parent) method of the child window. But I got a null exception somewhere in the JFace library:

Exception occurred java.lang.NullPointerException
at org.eclipse.jface.window.Window.initializeBounds(Window.java:758)
at org.eclipse.jface.window.Window.create(Window.java:435)
at org.eclipse.jface.window.Window.open(Window.java:790)

Where is a correct place to initialize the window widgets with initial values? Ideally I feel it should meet the conditions:

  • Widgets should be already created by the framework.
  • The window should not be visible yet.
  • close() should close the window correctly if needed.

If the Window object hasn't got a window shell yet, the open() method creates a new one and opens it. It's in its create() method when createContents(Composite parent) is called upon, along with some other method which configure some visual details. That's the initializeBounds() method, which relies on an already existing shell. If you close it, there you go, you got the exception.

Maybe you would want to overwrite the open() method itself:

public int open() {
   if (!condition)
        return super.open();    // as usual
    else
        return CANCEL;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM