Synchronisation of custom widgets in Vaadin 7.1 (global 'tick' event launcher) - java

I have made a custom Vaadin 7.1 widget which displays a timer. This timer gets an initial timing from the server (using a connector) but afterwards ticks further on itself using a separate ticking thread. When using multiple ones next to each other, including ones which run another logic (countdown, etc) they visibly get desynchronized and don't tick together.
A possible solution is to simply have the widget expect a method call (e.g. expect an event to be triggered) from a single separate thread in the code using the widget. Is there something like that offered by the Vaadin framework or what would be the recommended way to do this according to the Framework's standards?

Related

What does this error mean JXBrowser should only be constructed on the EDT

I have some code and when it executes, it throws a RuntimeException, saying:
JXBrowser should only be constructed on the EDT
it is stemming from when I'm creating a JXbrowser component
browser = (JXBrowser) browserFactory.create(true, WebBrowserType.JX);
What should I look for in fixing this error?
JavaFX (OpenJFX) is not thread-safe.
Like Swing, Vaadin, and most any other user-interface framework, you must limit all access to, and manipulation of, the widgets and other UI-related objects only from within the one thread dedicated to that framework.
Apparently your app is starting other threads and then performing JavaFX work on them. Never do this.
There are ways for you to perform lengthy tasks on background threads, and then upon completion post a request to the UI thread to update the UI widgets with the results. But you must study to learn those techniques.
See this tutorial and this one.
JxBrowser dropped this requirement since version 7.
In JxBrowser 7, you can initialize Engine and Browser in any thread. These operations are heavy and it's better not to do them in EDT.
Once the browser is created, you will need to create BrowserView and add it to your interface inside of EDT, according to Swing rules.

Busy popup in JavaFX when FXML parsing is too slow

FXML performance is an issue with JavaFX, if you have complex screens and have divided them into small components (for maintainability / reuse) that use FXML, then it can get really slow.
As FXML parsing is made in UI Thread (not sure of this, still it blocks the JavaFX Application Thread), you cannot show a glasspane / popup / etc in JavaFX when FXML is being processed.
The only workaround I found is to use a Swing popup (as it is in Swing UI Thread, you can still show something) to provide a feedback to the user (it is working / not a bug / wait a little more) when FXML is being loaded and to close it when no more FXML files are parsed.
I have built a facade above FXMLLoader to do so. Also this also works with OpenGL libraries as well (LWJGL for instance, instead of Swing, anything that is UI and is not in JavaFX Application Thread works).
I was wondering if a better solution exists (JavaFX only, not mixing UI frameworks) as this artificially adds complexity to the project and won't be ported well with OpenJFX ports.
Recommended Solution
Use JavaFX 8u40+, you can find an early access release.
For 8u40, the following bug was fixed:
RT-17716 Some controls can only be created on the FX application thread
This fix allows you to create all controls (except currently a WebView) off of the JavaFX application thread. That means you can load up your FXML asynchronously to the JavaFX application thread within a standard JavaFX Task. While the task is running you can have a please wait dialog or animated progress indicator displayed or whatever you like (in JavaFX, no need to use other frameworks like Swing/LWJGL).
My favorite way of handling this is to load the FXML elements up while the user is shown a login prompt or needs to create some input (but whether or not you use the "load stuff in the background while awaiting user input" trick is app dependent).
You can also load your FXML in the init function of your application, so that the FXML is loaded in parallel to the JavaFX system starting up (you need to take care around threading a bit for that to ensure that you don't actually try to show your scene until all the FXML is loaded and the operation to show the scene occurs on the JavaFX application thread).
Alternate Solution
You could also try this solution to Convert FXML to Java as part of the build, then perhaps there won't be any issue with slow loading of FXML (because there is no longer any FXML, it has been converted to Java). But I don't know if that solution is currently mature and stable enough for your purposes.

How to trigger a method to start at a certain?

I need a piece of code to start whenever it it 11 AM everyday. I do not want to use windows task manager or any other external things to start my app.
I need my app to trigger my method whenever it is time, 11 AM.
I have the following questions:
1) Is there any "Time Listener" (similar to action listener) that would do that
2) Is it ok if my app is always runs without closing
As others have suggested, you can use the Timer class in java (java.util.Timer) or for finer control the Quartz library (that library uses the Unix cron style).
And yes it is ok if your app runs without closing. You can run it at the background but you might be better adding notification icon in the taskbar to ensure the user can find your app when they need to.

Creating multiple windows in 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:
Do I need to listen for shell.dispose() in this case, or to only listen for display.dispose() in my main method?
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.
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.

Java - How to change cursor without Swing Component?

I have a Java application that run as a background service, i.e. no GUI. But when starting it, say through a batch script, I have some preparation works to be done at the very beginning of my program (something like communicate with server).
I want a busy cursor appear at this busy moment. Is it possible to do this without using any Swing component? Thanks for any suggestion or alternative approach.
I'd go for a console-based "progress bar". It's possible to use Swing but it would render your solution useless in a headless environment. Drawing progress bars with characters in terminal is quite a common practice.
You can either implement it yourself or use a ready class. Just take a look at github or bitbucket and you'll find something for sure.
A busy cursor could even be displayed as a caption with alternating last character (as simple as possible), with "frames" like these (rotating bar)
LOADING DATA /
LOADING DATA |
LOADING DATA \
LOADING DATA -
Implementing something like this wouldn't take much time. We're talking about minutes here.
If the only thing that prevents you from using Swing is an appearing window, you can change its appearance thanks to window translucency. This way you'd get an animated image overlaying your screen. It wouldn't work in a terminal though.
If you're able to use Java AWT (Abstract Window Toolkit) I beleive you can use the setCursor of Component class to change the cursor.
More on this : http://www.roseindia.net/java/example/java/awt/ChangeCursor.shtml

Categories