Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

JavaFX Tutorials for beginners

Author: Ganesh Swami
by Ganesh Swami
Posted: Jul 26, 2016
JavaFX CSS :
  • We can use cascading style sheets (CSS) to create a custom look for JavaFX GUI application.
  • We can apply CSS to scene,nodes and UI controls etc.
  • Using CSS in JavaFX applications is similar to using CSS in HTML.
  • So that we can change look and feel of our JavaFX GUI application using CSS.
JavaFX CSS Example :package javafxtuts; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; /** * * @author JavaFXtuts.com */ public class Javafxtuts extends Application { @Override public void start(Stage primaryStage) { HBox root = new HBox(); //Set space or padding using setPadding() method root.setPadding(new Insets(20)); //assiging a class to the button Button button=new Button("my button"); //Adding a class to the button button.getStyleClass().add("btn"); //assiging a class to the button1 Button button1 =new Button("Button1"); //set id to the button. button1.setId("btn1"); root.getChildren().addAll(button,button1); Scene scene = new Scene(root, 300, 150); //To add a external css file we do as String style= getClass().getResource("New.css").toExternalForm(); //now add the external css file to the scene scene.getStylesheets().add(style); primaryStage.setTitle("javafxtuts.com"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } } JavaFX JavaFX Button CSS :
  • You can apply CSS to the JavaFX button.
  • First of all, You should read JavaFX CSS and JavaFX Buttons Tutorials.
  • Now make a simple Button
  • Button button =new Button("My Button");
  • Now apply ID to the button as
  • button.setId("btn");
  • now defining the style sheet
  • And fanally add css file to the scene as
  • String style= getClass().getResource("New.css").toExternalForm(); scene.getStylesheets().add(style);
JavaFX CSS Button Example : package javafxtuts; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; /** * * @author JavaFXtuts.com */ public class Javafxtuts extends Application { @Override public void start(Stage primaryStage) { HBox root = new HBox(); //Set space or padding using setPadding() method root.setPadding(new Insets(20)); //assiging a class to the button Button button=new Button("my button"); //Adding a class to the button button.getStyleClass().add("btn"); //assiging a class to the button1 Button button1 =new Button("Button1"); //set id to the button. button1.setId("btn1"); root.getChildren().addAll(button,button1); Scene scene = new Scene(root, 300, 250); //To add a external css file we do as String style= getClass().getResource("New.css").toExternalForm(); //now add the external css file to the scene scene.getStylesheets().add(style); primaryStage.setTitle("javafxtuts.com"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } } CSS file for
About the Author

I am java lover, specially i like the JavaFX because it make easy to development of reach client applications. I currently working on JavaFX Tutorials and a JavaFX project.

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Ganesh Swami

Ganesh Swami

Member since: Jul 25, 2016
Published articles: 1

Related Articles