27

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.

6 Answers 6

19

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");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

You can also use Scala.js to compile scala to javascript.
You still have to bridge things from JVMWorld to JSWorld
That's true. To make it possible API provides wrappers for JavaScript primitive types (JSValue) and objects (JSObject).
To add jxbrowser isn't FOSS and asks $1,799 for a single developer license. This may make it a no go for some people.
Whew! I've been looking for something that would allow a native Java desktop app with HTML/JS/CSS support, but $1800? I guess I get it, but that sure is a hefty price point. I guess I'm just used to Apache licensed open source products.
7

It's not really feasible. 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.

If you absolutely need to have HTML/CSS as your UI framework and can't go to a server/client model, your best bet is probably looking at something like Google Native Client, but that uses C/C++ bindings on the backend. I haven't used Native Client so I can't personally give much more information on that front.

Edit to add:

One option is to embed a native browser into your Swing app using something like: http://djproject.sourceforge.net/ns/

There are some pure Java HTML renderers, however, they most likely won't be fully HTML5/CSS3 compliant, let alone possibly have Javascript bugs as well.

See here for some of those options: Pure Java HTML viewer/renderer for use in a Scrollable pane

1 Comment

Yep I think I will have to use a client-server model. The advantage is that people could use their own browser and they could remotely manage their locals git repos.
4

Like @Reverand Gonzo says, you will need some form of server/client. But you could easily embed a Jetty server into a Java app and then use GWT for your client code.

1 Comment

How would the user access UI? Via web browser?
2

You can bring in the power of HTML,CSS,JavaScript into your Swing app using JFXPanel to embed JavaFX WebView. Have a look at the SimpleSwingBrowser demo in this link:https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/swing-fx-interoperability.htm

WebView allows to call JavaScript functions from Java and vice versa. It is also a nice way to enhance your legacy Java app with web technologies.

Comments

1

JavaFX 2.2 brought this functionality to providing a user interface component (GUI) that has web view and full browsing functionality.

For more details, see Adding HTML Content to JavaFX Applications.

Comments

0

Use Angular.js with HTML and rest of the things as same in Java, just use classes for business logic, no need to write code for awt/swing. Angular with spring boot are rapid development in Java for webapp with less code in Java without swing use to create best webapp .

1 Comment

This is a very broad answer. Please try to include some explanation or details about what you're recommending. For example: "just use classes for business logic". That's so broad as to be essentially useless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.