• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 https://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.
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome! JavaFX looks like exactly what I was looking for! Thanks a bunch guys!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Are you okay? You look a little big. Maybe this tiny ad will help:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic