'Java Swing Application to Browser Application [closed]
I have created a fairly complicated Java Swing GUI program. It’s essentially a modified board game and uses things like SwingWorker, MouseListener, children of JComponents. I am looking for a way to convert this program so that it can be run on a browser (not to have multiplayer online with servers and what not). Does anyone have any advice on how to approach this? I did some quick googling and found Vaadin and webswing but I do not know if they contain all the features I need or if it’s worth the effort to do it with them.
Solution 1:[1]
Since you explicitly mentioned Vaadin as an example, I will elaborate on what it brings to the table. Vaadin uses a programming model that is very similar to that of Swing. What this means is that you can in general structure your code in the same way, i.e. by creating and configuring components as Java instances, adding those components as children of layout components and finally listening to events from those components.
The actual component implementations are different than in Swing, but the same basic set of components such as buttons, text input fields and data grids are still available. The different component implementations means that you will still end up rewriting all of the code related to the actual GUI.
The benefit of Vaadin in this case is that you don't need to learn how browsers work just to implement something that runs natively in the browser. You can also reuse all your existing non-GUI code with relatively small changes. On the other hand, you can also see this as an opportunity to get familiar with native web technologies such as HTML and JavaScript since that's a very useful skill to have.
Solution 2:[2]
What you're describing is a java applet, which is no longer a viable platform.
If you want to use java for application logic but not the interface, you might consider a frontend library that connects to a java backend using websockets or ajax calls.
If you absolutely must use Swing, then you're only option is to render the application on a server and then send 'screen shots' or maybe draw calls back to the browser and have it render them to a canvas using javascript, which is essentially what the libraries you found do. It's slow and glitchy, and likely not very scalable.
Another option is packaging the java application into an exe or jar and running it on the remote system all on its own but delivering it through the browser.
Solution 3:[3]
If you are just interested in how your Swing application will work in a web browser you should definitely give Webswing a try. I disagree with Charlie that the "libraries are slow, glitchy and not scalable". Webswing is fast and renders exactly same as on a native platform. This quickstart guide will help you setup your application.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Leif Åstrand |
| Solution 2 | Charlie |
| Solution 3 | Branislav Kuliha |
