简体   繁体   中英

JavaFX - CSS can't be load in Linux

I'm using JavaFX to develop a multiplatform application and I'm using Win7 with Netbeans 7.2.1 with JavaFX 2.2 and Java 1.7u21 (previously using 1.7u9 and working great too). As I said, Win7 is running just fine my app. When I try to run it on Ubuntu 12.04, it simply can't load my CSS styles. Every fxml file has its control's stylesheet classes added. And then every control has it style added. For example: on a blank button, I'm applying this style:

buttonStyle.css:

.button {
    -fx-text-fill: white;
    -fx-alignment: CENTER;
    -fx-border-radius: 5;
    -fx-font-size: 13pt;
    -fx-font-family: "Verdana";
    -fx-font-weight: bold;
    -fx-background-color: linear-gradient(#328BDB 0%, #207BCF 25%, #1973C9 75%, #0A65BF 100%);
}

.button:hover {
    -fx-background-color: linear-gradient(#2C8AE7 0%, #0C6AC7 25%, #0A58A5 75%, #093B6D 100%);
}

.button:pressed {
    -fx-background-color: linear-gradient(#328BDB 0%, #207BCF 25%, #1973C9 75%, #0A65BF 100%);
}

On Win7 works fine but it generate the following exception on Ubuntu:

java.lang.ClassNotFoundException: AUTHOR Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.javafx.main.Main.launchApp(Main.java:642) at com.javafx.main.Main.main(Main.java:805) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403) at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47) at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115) at java.lang.Thread.run(Thread.java:722) Caused by: java.lang.ArrayIndexOutOfBoundsException: 512 at com.sun.javafx.css.StyleConverter.readBinary(StyleConverter.java:99) at com.sun.javafx.css.ParsedValue.readBinary(ParsedValue.java:600) at com.sun.javafx.css.Declaration.readBinary(Declaration.java:142) at com.sun.javafx.css.Rule.readBinary(Rule.java:189) at com.sun.javafx.css.Stylesheet.readBinary(Stylesheet.java:185) at com.sun.javafx.css.Stylesheet.loadBinary(Stylesheet.java:215) at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:572) at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:411) at com.sun.javafx.css.StyleManager.access$1900(StyleManager.java:72) at com.sun.javafx.css.StyleManager$StylesheetContainer.gatherParentStylesheets(StyleManager.java:1140) at com.sun.javafx.css.StyleManager$StylesheetContainer.getStyleHelper(StyleManager.java:1225) at com.sun.javafx.css.StyleManager$StylesheetContainer.access$1300(StyleManager.java:1031) at com.sun.javafx.css.StyleManager.getStyleHelper(StyleManager.java:968) at javafx.scene.Node.impl_createStyleHelper(Node.java:7433) at javafx.scene.Node.impl_processCSS(Node.java:7403) at javafx.scene.Parent.impl_processCSS(Parent.java:1146) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Node.processCSS(Node.java:7383) at javafx.scene.Scene.doCSSPass(Scene.java:443) at javafx.scene.Scene.preferredSize(Scene.java:1445) at javafx.scene.Scene.impl_preferredSize(Scene.java:1512) at javafx.stage.Window$10.invalidated(Window.java:719) at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127) at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161) at javafx.stage.Window.setShowing(Window.java:782) at javafx.stage.Window.show(Window.java:797) at javafx.stage.Stage.show(Stage.java:229) at com.test.MainInterface.start(MainInterface.java:62) at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206) at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82) ... 1 more

Removing buttonStyle.css and adding comboStyle.css, the following exception is thrown:

comboStyle.css:

.combo-box {
    -fx-font: 13pt "Verdana";
    -fx-alignment: baseline-left;
    -fx-background-color: linear-gradient(#328BDB 0%, #207BCF 25%, #1973C9 75%, #0A65BF 100%);
}

.combo-box .text {
    -fx-fill: white;
}

.combo-box .arrow-button {
    -fx-background-color: linear-gradient(#328BDB 0%, #207BCF 25%, #1973C9 75%, #0A65BF 100%);
}

.combo-box .arrow {
    -fx-background-color: white;
}

Exception: java.io.EOFException at java.io.DataInputStream.readByte(DataInputStream.java:267) at com.sun.javafx.css.CompoundSelector.readBinary(CompoundSelector.java:351) at com.sun.javafx.css.Selector.readBinary(Selector.java:113) at com.sun.javafx.css.CompoundSelector.readBinary(CompoundSelector.java:344) at com.sun.javafx.css.Selector.readBinary(Selector.java:113) at com.sun.javafx.css.Rule.readBinary(Rule.java:182) at com.sun.javafx.css.Stylesheet.readBinary(Stylesheet.java:185) at com.sun.javafx.css.Stylesheet.loadBinary(Stylesheet.java:215) at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:572) at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:411) at com.sun.javafx.css.StyleManager.access$1900(StyleManager.java:72) at com.sun.javafx.css.StyleManager$StylesheetContainer.gatherParentStylesheets(StyleManager.java:1140) at com.sun.javafx.css.StyleManager$StylesheetContainer.getStyleHelper(StyleManager.java:1225) at com.sun.javafx.css.StyleManager$StylesheetContainer.access$1300(StyleManager.java:1031) at com.sun.javafx.css.StyleManager.getStyleHelper(StyleManager.java:968) at javafx.scene.Node.impl_createStyleHelper(Node.java:7433) at javafx.scene.Node.impl_processCSS(Node.java:7403) at javafx.scene.Parent.impl_processCSS(Parent.java:1146) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Node.processCSS(Node.java:7383) at javafx.scene.Scene.doCSSPass(Scene.java:443) at javafx.scene.Scene.preferredSize(Scene.java:1445) at javafx.scene.Scene.impl_preferredSize(Scene.java:1512) at javafx.stage.Window$10.invalidated(Window.java:719) at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127) at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161) at javafx.stage.Window.setShowing(Window.java:782) at javafx.stage.Window.show(Window.java:797) at javafx.stage.Stage.show(Stage.java:229) at com.test.MainInterface.start(MainInterface.java:62) at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206) at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82) at java.lang.Thread.run(Thread.java:722) java.io.EOFException at java.io.DataInputStream.readByte(DataInputStream.java:267) at com.sun.javafx.css.CompoundSelector.readBinary(CompoundSelector.java:351) at com.sun.javafx.css.Selector.readBinary(Selector.java:113) at com.sun.javafx.css.CompoundSelector.readBinary(CompoundSelector.java:344) at com.sun.javafx.css.Selector.readBinary(Selector.java:113) at com.sun.javafx.css.Rule.readBinary(Rule.java:182) at com.sun.javafx.css.Stylesheet.readBinary(Stylesheet.java:185) at com.sun.javafx.css.Stylesheet.loadBinary(Stylesheet.java:215) at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:572) at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:411) at com.sun.javafx.css.StyleManager.access$1900(StyleManager.java:72) at com.sun.javafx.css.StyleManager$StylesheetContainer.gatherParentStylesheets(StyleManager.java:1140) at com.sun.javafx.css.StyleManager$StylesheetContainer.getStyleHelper(StyleManager.java:1225) at com.sun.javafx.css.StyleManager$StylesheetContainer.access$1300(StyleManager.java:1031) at com.sun.javafx.css.StyleManager.getStyleHelper(StyleManager.java:968) at javafx.scene.Node.impl_createStyleHelper(Node.java:7433) at javafx.scene.Node.impl_processCSS(Node.java:7403) at javafx.scene.Parent.impl_processCSS(Parent.java:1146) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Parent.impl_processCSS(Parent.java:1153) at javafx.scene.Node.processCSS(Node.java:7383) at javafx.scene.Scene.doCSSPass(Scene.java:443) at javafx.scene.Scene.preferredSize(Scene.java:1445) at javafx.scene.Scene.impl_preferredSize(Scene.java:1512) at javafx.stage.Window$10.invalidated(Window.java:719) at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127) at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161) at javafx.stage.Window.setShowing(Window.java:782) at javafx.stage.Window.show(Window.java:797) at javafx.stage.Stage.show(Stage.java:229) at com.test.MainInterface.start(MainInterface.java:62) at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206) at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82) at java.lang.Thread.run(Thread.java:722)

Any idea why are those styles can't be loaded in Linux?

I just found a way to solve it. I was setting all CSS references on my FXML files. Also, I was reading an article about converting CSS files into Binary files (.bss), automatically. Then I've been researching a way to cancel this convertion on my Maven project, like bellow:

<plugin>
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>1.5</version>
    <configuration>
        <mainClass>package.mainClass</mainClass>
        <css2bin>false</css2bin> <!-- THIS LINE -->

        ...
    </configuration>
</plugin>

Other kind of projects can use -Dbinary.css=false as running argument to avoid this convertion.

Finally, I just set all my CSS loading on my Controller classes using:

anchorPaneScene1.getStylesheets().add("/path/to/resources/css/button.css")
button1.getStyleClass().add("button");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM