Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating multiple windows in SWT

Tags:

java

swt

I'm trying to create a Window class which I can use to open multiple windows, and which will automatically add an event handler to listen for the Swt.CLOSE event, and call the shell.dispose() method when it is called.

My questions are:

  1. Do I need to listen for shell.dispose() in this case, or to only listen for display.dispose() in my main method?

  2. Do I need to run each window in its own thread, or can all the windows share the same UI thread? I've read some reports of buggy behavior related to event handling in case of multiple windows being open.

like image 801
Ali Avatar asked Apr 30 '26 22:04

Ali


1 Answers

I recommend you should always have a single UI thread, which the single Display object runs on. See SWT: single vs. multiple displays or even the Eclipse documentation on Display that strongly recommends using a single Display object:

Applications which are built with SWT will almost always require only a single display. In particular, some platforms which SWT supports will not allow more than one active display.

There are even several sample apps available (such as this one) that demonstrate multiple shells in SWT. Calling shell.dispose() when you want to close a window is the way to go.

You should only use display.dispose() when you are shutting down the entire app, basically as a 'last step' - see this example, or this one, or pretty much any snippet on the SWT Snippets page.

Edit

The Eclipse framework itself is an example of an application that can have multiple windows - it still uses a single Display, with a single UI Thread and shared event system. Eclipse documentation on Threading Issues has a basic explanation of this:

Underneath any GUI application, regardless of its language or UI toolkit, the OS platform detects GUI events and places them in application event queues. [...] It determines which window and application should receive each event and places it in the application's event queue.

like image 122
Krease Avatar answered May 03 '26 10:05

Krease



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!