Table of contents
1.
Introduction
2.
JavaFX 
3.
JavaFX Selectors
4.
Example 
4.1.
Personal Information Form
4.2.
Output
5.
Example 
5.1.
Employee Information Form
5.2.
Output
6.
Frequently Asked Question 
6.1.
Without Java, can I still use JavaFX?
6.2.
What is the package?
6.3.
What is the use of CSS animations?
6.4.
Is JavaFX taking the place of Swing as Java SE's new client UI library?
6.5.
What is a CSS tag selector?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Selectors

Author Muskan Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

While working on a web application, a programmer needs to enhance the appearance of the application to make it more interactive. So JavaFX provides different classes to apply different CSS effects to the web application. So, in this blog, we will be learning one of the classes, JavaFX Selectors. Let's explore more about it.

But before that, let's have a look at JavaFX.

JavaFX 

JavaFX is a GUI toolkit and Java framework created to support the creation of desktop, online, and Rich Internet Application (RIA) software. The main benefit of utilizing JavaFX is that programmes created with it may run on various platforms, including desktops, the web, mobile phones, TVs, tablets, and operating systems like Windows, Linux, iOS, and Android. Due to its ability to run across various platforms and operating systems, the JavaFX library possesses this feature.

JavaFX Selectors

Locating nodes on the JavaFX scene graph typically requires the use of JavaFX selectors. The user can modify the styles of the nodes on the scene graph using the JavaFX selectors.

JavaFX's CSS is contained in a single file. It outlines each UI control's style. The stylistic guidelines are divided into three primary categories:

  1. Selector: This refers to the HTML tag that the style is primarily applied. There are other selectors, including <table> and <form>.
  2. Property: The attribute type for an HTML tag is described by the property. The CSS has characteristics like border, color, and font.
  3. Value: In CSS, specific attributes may be given a value by the user. Red, or the color code #FFFFFF, is a color attribute.


Let’s have a look at an example for more clarity.

Example 

Personal Information Form

Style.css

.label  
{  
    -fx-background-color:Green;   
    -fx-text-fill:white;   
    -fx-font-size:16;  
    -fx-border-color: Black;  
    -fx-border-radius: 7;  
}  
.button  
{  
    -fx-background-color:Orange;   
    -fx-font-family:courier_new;   
    -fx-text-fill:White;  
    -fx-border-color: Wheat;  
    -fx-border-radius: 5;  
    -fx-font-size:16;  
}

 

Ninjas.java

import javafx.application.Application;    
import javafx.scene.Scene;    
import javafx.scene.control.Button;    
import javafx.scene.control.Label;    
import javafx.scene.control.TextField;    
import javafx.scene.layout.GridPane;    
import javafx.stage.Stage;  
  
public class Ninjas extends application {   
  
@Override  
public void start(Stage primaryStage) throws Exception {    

Label first_name=new Label("First Name");    
Label last_name=new Label("Last Name");    
TextField tf1=new TextField();    
TextField tf2=new TextField();    
Button Submit=new Button ("Submit");     
GridPane root=new GridPane();    
root.setHgap(10);  
root.setVgap(15);  
Scene scene = new Scene(root,400,200);    
root.addRow(0, first_name,tf1);    
root.addRow(1, last_name,tf2);    
root.addRow(2, Submit);    
//Adding CSS file to the root   
root.getStylesheets().add("style.css");  
primaryStage.setScene(scene);
primaryStage.setTitle("Coding Ninjas");   
primaryStage.show();    
}    
public static void main(String[] args) {    
launch(args);    
}    
  
}

Output

 

Example 

Employee Information Form

 

Style.css

.label  
{  
    -fx-background-color:Green;   
    -fx-text-fill:white;   
    -fx-font-size:16;  
    -fx-border-color: Black;  
    -fx-border-radius: 7;  
}  
.button  
{  
    -fx-background-color:Orange;   
    -fx-font-family:courier_new;   
    -fx-text-fill:White;  
    -fx-border-color: Wheat;  
    -fx-border-radius: 5;  
    -fx-font-size:16;  
}

 

Ninjas.java

import javafx.application.Application;    
import javafx.scene.Scene;    
import javafx.scene.control.Button;    
import javafx.scene.control.Label;    
import javafx.scene.control.TextField;    
import javafx.scene.layout.GridPane;    
import javafx.stage.Stage;    
public class Ninjas extends Application {   
  
@Override  
public void start(Stage primaryStage) throws Exception {    
Label country=new Label("Emp. Name");    
Label state=new Label("Job Location");    
TextField tf1=new TextField();    
TextField tf2=new TextField();    
Button Submit=new Button ("Submit");     
GridPane root=new GridPane();    
root.setHgap(10);  
root.setVgap(15);  
Scene scene = new Scene(root,400,200);    
root.addRow(0, emp,tf1);    
root.addRow(1, job,tb2);    
root.addRow(2, Submit);    
root.getStylesheets().add("style.css");  
primaryStage.setScene(scene);    
primaryStage.setTitle("CodingNinjas");
primaryStage.show();    
}    
public static void main(String[] args) {    
launch(args);    
}    
  
}

Output

Frequently Asked Question 

Without Java, can I still use JavaFX?

No. JavaFX apps cannot be run on a PC without the Java Runtime Environment (JRE), and JavaFX Runtime installed.

What is the package?

A Java package is a collection of sub-packages, interfaces, and classes of a common type.

What is the use of CSS animations?

Transitions from one CSS style setting to another can be animated with the help of CSS animations.

Is JavaFX taking the place of Swing as Java SE's new client UI library?

Yes. To be included in the JRE, Swing will continue to be a part of the Java SE specification for the foreseeable future. Although we advise programmers to use JavaFX APIs as much as possible when creating new applications, it is feasible to add JavaFX functionality to an existing Swing application, facilitating a more seamless transition.

What is a CSS tag selector?

It is a set of phrases and elements that instruct the browser which HTML elements to pick so that the values of the CSS property values in the rule are applied to them.

Conclusion

Finally, you've concluded this article. 

Congratulations!! You learned about JavaFX Selector in this blog. You studied the different CSS effects we can apply to the web application. After reading this, are you eager to read more articles on the subject of JavaFX ? Don't worry; Coding Ninjas will take care of everything. 

Check out the awesome content on the Coding Ninjas Website:

JavaFX Inline Styles

JavaFX Menu

Please see our Code studio guided routes to learn more about Java, Competitive Programming, JavaScript, System Design, and other topics. Also, enroll in our courses and use the accessible sample tests and problems. For placement preparations, have a look at the interview experiences and interview bundle.

Do upvote our blog to help other ninjas grow. Happy Coding!

 

Live masterclass