You can
add styles to a JavaFX button by using css styles. Below step by step procedure
shows you how to add styles to a button using style sheets.
Step 1: Define styles in a css file. For
example, I defined in demo.css file
demo.css
.buttonClass1{
-fx-font: 40 arial;
-fx-base: #00ff00;
}
Step 2: Add the css file to Scene object
Scene
scene = new Scene(vBox, 400, 400);
scene.getStylesheets().add(new
File(cssFilePath).toURI().toURL().toExternalForm());
Step 3: Add the style to Button instance.
Button
button = new Button("Click Me");
button.getStyleClass().add("buttonClass1");
Find the
below working application.
package com.sample.demos; import static javafx.geometry.Pos.CENTER; import java.io.File; import java.io.FileNotFoundException; import java.net.MalformedURLException; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class ButtonApp extends Application { private static String cssFilePath = "C:\\Users\\krishna\\Documents\\Study\\javaFX\\demo.css"; @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { Button button = new Button("Click Me"); button.getStyleClass().add("buttonClass1"); VBox vBox = new VBox(10, button); vBox.setAlignment(CENTER); Scene scene = new Scene(vBox, 400, 400); scene.getStylesheets().add(new File(cssFilePath).toURI().toURL().toExternalForm()); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX Button Example"); primaryStage.setScene(scene); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(ButtonApp.class, args); } }
No comments:
Post a Comment