Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HTML and CSS as a Java application GUI?

Tags:

I want to design new Git client with a clean GUI.

Is it possible to use the power of HTML, CSS and JavaScript in a java application?

I would like to use Java + JGit for models, Java for controllers and HTML + CSS + JavaScript for views.

I don't want a client-server model. I would like to integrate Java and HTML nicely. A DOM event would fire events directly to a Java controller. This way it would be possible to create rich offline application.

like image 802
Guillaume Massé Avatar asked Aug 24 '11 14:08

Guillaume Massé


People also ask

Can HTML and CSS be used with Java?

Rich clients in Java are done using Swing or SWT. If you want to use HTML/CSS for your user interface, you need to use the server/client model. It can be as simple as creating a local server and launching a browser that connects to it, but it would still be that model.

Can we use HTML in Java Swing?

HTML formatting can be used in all Swing buttons, menu items, labels, tool tips, and tabbed panes, as well as in components such as trees and tables that use labels to render text.

Can you use HTML for a GUI?

With JxBrowser your Java desktop application GUI can be built with HTML/CSS/JavaScript. It means that you can actually use any modern HTML5 UI toolkit to build modern, user-friendly interface of your Java desktop application. You do not need to hire Swing/AWT/JavaFX developers.

Can you build GUI with Java?

GUI, which stands for Graphical User Interface, is a user-friendly visual experience builder for Java applications. It comprises graphical units like buttons, labels, windows, etc. via which users can connect with an application. Swing and JavaFX are two commonly used applications to create GUIs in Java.


1 Answers

You can embed web browser component into your Java Swing/JavaFX Desktop application that displays GUI built with HTML5+CSS+JavaScript. You can see an article that describes how to do this at https://jxbrowser-support.teamdev.com/docs/tutorials/cross-desktop-apps.html

One of the Java Swing/JavaFX libraries that allows embedding Chromium into Java applications is JxBrowser. Using JxBrowser API you can load any web page and work with its DOM and JavaScript. You can even call Java methods from JavaScript code and vice versa. For example:

import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.JSFunctionCallback; import com.teamdev.jxbrowser.chromium.JSObject; import com.teamdev.jxbrowser.chromium.JSValue; import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent; import com.teamdev.jxbrowser.chromium.events.LoadAdapter;  public class JavaScriptJavaSample {     public static void main(String[] args) {         Browser browser = new Browser();         browser.addLoadListener(new LoadAdapter() {             @Override             public void onFinishLoadingFrame(FinishLoadingEvent event) {                 if (event.isMainFrame()) {                     Browser browser = event.getBrowser();                     JSObject window = (JSObject)                             browser.executeJavaScriptAndReturnValue("window");                     window.setProperty("MyFunction", new JSFunctionCallback() {                         @Override                         public Object invoke(Object... args) {                             for (Object arg : args) {                                 System.out.println("arg = " + arg);                             }                             return "Hello!";                         }                     });                     JSValue returnValue = browser.executeJavaScriptAndReturnValue(                             "MyFunction('Hello JxBrowser!', 1, 2, 3, true);");                     System.out.println("return value = " + returnValue);                 }             }         });         browser.loadURL("about:blank");     } } 
like image 88
Vladimir Avatar answered Sep 23 '22 23:09

Vladimir