• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a way to use HTML/CSS/JS as a GUI for Java?  RSS feed

 
Nicholas d'Hondt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is purely theoretically at the moment, I'm just curious, if it could be done, how much work would it be?

I'm not very comfortable with Swing or any other form of built-in GUI I know of for Java, but I am very comfortable with JavaScript, HTML, and CSS so I thought to myself, "What if I could combine the two?" and after doing some looking and thinking, I think it would be possible, but I don't know if there is anyway the Web languages could communicate with the Java.

Obviously, if I wanted to, I could create my own web browser using java (at least I think), but that would be way too much work for me, and why do that when there are perfectly good ones?

The problem is that I don't know enough about Java or Javascript. Which is why I am here.

Can I send info from client-side Java to client-side Javascript? I don't want to have to upload it to a server; I'd like to just be able to execute my Java program and have it open a web browser and display the HTML/CSS/JS GUI that is run by my Java program.
 
Tim Cooke
Marshal
Posts: 3704
197
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas, welcome to the Ranch!

What you need to read up on are Servlets and Ajax.

- Servlets enable your web application to communicate with a Java application through web form submissions.
- Ajax allows your JavaScript code to communicate with a Java application. I'd recommend using a framework such as jQuery to help you with this.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

If your app is a Java client app, then there is a more straightforward approach that offers much better Java/Web integration: the WebView component we discussed in http://www.coderanch.com/t/636322/JavaFX/java/websites-JavaFX.

An alternative approach would be for the desktop app to fire up an embedded servlet container (like Tomcat or Jetty) that runs your web app, and then to point your browser at its localhost address.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaFX desktop application framework (which is "next gen" Swing as per Oracle) has a WebView embedded browser control that allows exactly this.
You can execute javascript snippets on the page from java, as well as call back into java from javascript.
There's a good tutorial here.
This enables a user interface where user can click typical desktop controls - buttons, menus, etc - to perform actions on the web page.

It's an alternative solution to using servlets/Apache HttpComponents + ajax.

Update: Ah, Ulf has already said what I had in mind while I was drafting. I'll leave the reply here since you may find the links useful.
 
Nicholas d'Hondt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! JavaFX looks like exactly what I was looking for! Thanks a bunch guys!
 
Anthony Ogg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just developed an open source platform that answers exactly this question: jhod

With this, you can develop Desktop applications, using HTML for frontend, and regular Java JAX-RS web-services for your processes.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting question. I also want to know abit of it.
Does it means that if I do GUI as html- the GUI to work is required internet connection?
I simply wish it to be replacement of swing.
 
Anthony Ogg
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, all your html GUI code is local (it's a NW.js application) and it will be able to exchange data with a localhost server (served by an embedded web server) that will provide an API using java JAX-RS code, so you don't need any internet connection.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!