How to use external css file to style a javafx application


Question

i want to style my javafx application with an external css file but when i am adding css file, it is not creating any difference. i am using Netbeans 7.4 IDE and jdk8, although the code is not pointing any error or exception but i am not getting the required output. I am totaly confused what to do. My code is...

package manualstyle;

import java.io.File;
import java.net.URL;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

/**
 *
 * @author vickyjonnes
 */
public class ManualStyle extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root=new Group();
        Scene scene = new Scene(root, 300, 250);

        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setLayoutX(100);
        btn.setLayoutY(100);
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });



        root.getChildren().add(btn);
        primaryStage.setScene(scene);
        String css = ManualStyle.class.getResource("myStyle.css").toExternalForm();
        scene.getStylesheets().add(css);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

I have a css file which resides in the same directory and the content of css file is :

.root{
    -fx-background-color: #ff0066;
 }
1
1
2/27/2014 11:28:35 AM

Accepted Answer

Please go through the following solution. Let me know, if you still face issues :

https://stackoverflow.com/a/22048338/1759128

2
5/23/2017 11:45:22 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon