2017-06-07 11 views
2

スタイルシートファイルではなくJavaコードでスタイルクラスを作成して、javafxノードに追加したいとします。javafxで動的にスタイルクラスを作成して追加するには

+0

:私はでsetStyle方法について知っているが、この場合には、私はそれを使用することはできません。 – user2858883

+0

なぜそれは使えませんか? – MeGoodGuy

+0

これを見てください:https://stackoverflow.com/questions/16236641/dynamically-add-css-stylesheets-in-javafx – MeGoodGuy

答えて

1

ない、これはあなたが探しているものであるかどうかわから...

Button node = new Button(); 
node.getStyleClass().add("my-new-style-class"); 

.my-new-style-class { 
    -fx-padding: 5; 
} 
+0

いいえ、CSSではなくコードでスタイルクラスを定義します。 – user2858883

0

アイデアは、一時的なstlyesheetファイルを作成して内部の新しいスタイル・クラスを作成し、リストにスタイルシートを追加することですノードのシートを追加し、新しいスタイルクラスも追加します。ここで

は実施例である:レコードの

Button button = new Button("My Text"); 

button.setOnAction(e -> { 

    try { 
     // Create a new tempfile that will be removed as the application exits 
     File tempStyleClass = File.createTempFile("AppXY_TempStyleClass", ".css"); 
     tempStyleClass.deleteOnExit(); 

     // Write the stlye-class inside 
     try (PrintWriter printWriter = new PrintWriter(tempStyleClass)) { 
      printWriter.println(".temp-style { -fx-text-fill: red; }"); 
     } 

     // Add the style-sheet and the style-class to the node 
     button.getStylesheets().add(tempStyleClass.toURI().toString()); 
     button.getStyleClass().add("temp-style"); 

    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 
}); 
関連する問題