How to Use CSS for Java Swing

Can I use CSS for Java Swing?

http://code.google.com/p/flying-saucer/
Flying Saucer takes XML or XHTML and applies CSS 2.1-compliant stylesheets to it, in order to render to PDF (via iText), images, and on-screen using Swing or SWT

Use CSS in Java Applications

You should look up javafx. You can attach a css file to a GUI. it is fairly easy to use. I can give you some example code if you want.

These are some good tutorials on how to use it: https://www.youtube.com/watch?v=FLkOX4Eez6o

I use eclipse to create a css file right click the project -> select new -> other -> CSS file

import javafx.application.*;
import java.text.*;
import java.util.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.input.*;
import javafx.stage.*;
import javafx.geometry.*;

public class Display extends Application{

public static void main(String[] args){
launch(args);
}

@Override
public void start(Stage stage) throws Exception{


HBox root = new HBox();

Button button = new Button("button");
Label label = new Label("Label");

root.getChildren().addAll(button, label);

Scene scene = new Scene(root, 700,300);
scene.getStylesheets().add("Style.css");
stage.setScene(scene);

stage.setTitle("Title");

stage.show();
}
}

<---------------------------------CSS File------------------------------>

 .root{
-fx-background-color: linear-gradient(#383838, #838383);
-fx-font-size: 11pt;
}

.label{
-fx-text-fill: #e8e8e8;
}

.button{
-fx-background-color: linear-gradient(#dc9556, #ab4642);
-fx-background-radius: 100;
}

.text-area{
-fx-background-color: white;
-fx-background-radius: 0;
}

Swing style with CSS

Swing will be deprecated and replaced by JavaFX in the long run. If you are building a new application, why don't you look into skinning JavaFX with CSS?

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

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");
}
}


Related Topics



Leave a reply



Submit