JavaFX – DatePicker

JavaFX – DatePicker ”; Previous Next The Date picker is a control that allows the user to select a date from a graphical calendar. It can be customized to display different date formats, restrict the range of selectable dates, and handle various events related to date selection. The figure below shows a typical datepicker − Creating DatePicker in JavaFX In JavaFX, the the date picker is represented by a class named DatePicker. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a date picker in JavaFX. Constructors of the DatePicker class are listed below − DatePicker() − It is the default constructor that constructs a date picker without any pre-defined date. DatePicker(LocalDate date) − It creates a new date picker with the specified date. We can use any of the above mentioned constructors to embed a DatePicker within JavaFX application. The most freuently used is the default constructor of the DatePicker class which does not accept any arguments. Once we instantiate this class, our next step would be setting the initial date value. Following that, instantiate any layout pane to hold the DatePicker. Lastly, set the scene and stage to display the date picker on the screen. Example The following JavaFX program demonstrates how to create a datepicker in a JavaFX application. Save this code in a file with the name DatepickerDemo.java. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.DatePicker; import javafx.scene.layout.VBox; import javafx.stage.Stage; import java.time.LocalDate; import javafx.scene.control.Label; import javafx.scene.paint.Color; import javafx.geometry.Pos; public class DatepickerDemo extends Application { @Override public void start(Stage stage) { // Creating a Label Label label = new Label(“Please Enter preferred Date: “); // Create a DatePicker and set its initial value DatePicker datePicker = new DatePicker(); datePicker.setValue(LocalDate.of(2020, 1, 1)); // Create a Label to display the selection Label selectLabel = new Label(); selectLabel.setTextFill(Color.RED); // Add a listener to the value property of the DatePicker datePicker.valueProperty().addListener((observable, oldValue, newValue) -> { // Print the selected date selectLabel.setText(“You selected: ” + newValue); }); // Creating a VBox to hold all controls VBox root = new VBox(); root.setAlignment(Pos.CENTER); root.setSpacing(10); root.getChildren().addAll(label, datePicker, selectLabel); // Create a Scene and set it to the Stage Scene scene = new Scene(root, 400, 300); stage.setTitle(“DatePicker in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } To compile and execute the saved Java file from the command prompt, use the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls DatepickerDemo.java java –module-path %PATH_TO_FX% –add-modules javafx.controls DatepickerDemo Output When we execute the above code, it will generate the following output. Showing week numbers in the Date Picker By default, the DatePicker does not show week numbers in the calendar. However, if there is a need to display week numbers along with the date, then we can set the setShowWeekNumbers() method to true as shown in the next example. Save this code in a file with the name DatepickerDemo.java. Example import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.DatePicker; import javafx.scene.layout.VBox; import javafx.stage.Stage; import java.time.LocalDate; import javafx.scene.control.Label; import javafx.scene.paint.Color; import javafx.geometry.Pos; public class DatepickerDemo extends Application { @Override public void start(Stage stage) { // Creating a Label Label label = new Label(“Please Enter preferred Date: “); // Create a DatePicker and set its initial value DatePicker datePicker = new DatePicker(); datePicker.setValue(LocalDate.of(2024, 1, 1)); datePicker.setShowWeekNumbers(true); // Create a Label to display the selection Label selectLabel = new Label(); selectLabel.setTextFill(Color.RED); // Add a listener to the value property of the DatePicker datePicker.valueProperty().addListener((observable, oldValue, newValue) -> { // Print the selected date selectLabel.setText(“You selected: ” + newValue); }); // Creating a VBox to hold all controls VBox root = new VBox(); root.setAlignment( Pos.BASELINE_CENTER); root.setSpacing(10); root.getChildren().addAll(label, datePicker, selectLabel); // Create a Scene and set it to the Stage Scene scene = new Scene(root, 400, 300); stage.setTitle(“DatePicker in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } Compile and execute the saved Java file from the command prompt using the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls DatepickerDemo.java java –module-path %PATH_TO_FX% –add-modules javafx.controls DatepickerDemo Output On executing, the above program generates a JavaFX window displaying a DatePicker with the week numbers as shown below − Print Page Previous Next Advertisements ”;

JavaFX – Light.Distant Effect

JavaFX – Light.Distant Effect ”; Previous Next On applying the light distant effect to a node, a light is simulated on it, as if it is being generated by a distant light source. Distant Light Source − A source which is at a far distance from the node. In here, the light is attenuated in one direction from the source. The class named Light.Distant of the package javafx.scene.effect represents the distant light source. This class contains two properties, which include − azimuth − This property is of the type double and it represents the azimuth of the light. elevation − This property is of the type double and it represents the elevation of the light. Example The following program is an example demonstrating the lighting effect of JavaFX. In this, the source of light is a distant source. Here, we are drawing a text “Welcome to Tutorialspoint” and a circle in a scene. To these, we are applying the lighting effect, where the light is being emitted by a distant source. Save this code in a file with the name DistantLightingExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.Light; import javafx.scene.effect.Lighting; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class DistantLightingExample extends Application { @Override public void start(Stage stage) { //Creating a Text object Text text = new Text(); //Setting font to the text text.setFont(Font.font(null, FontWeight.BOLD, 40)); //setting the position of the text text.setX(60); text.setY(50); //Setting the text to be embedded. text.setText(“Welcome to Tutorialspoint”); //Setting the color of the text text.setFill(Color.RED); //Drawing a Circle Circle circle = new Circle(); //Setting the center of the circle circle.setCenterX(300.0f); circle.setCenterY(160.0f); //Setting the radius of the circle circle.setRadius(100.0f); //setting the fill color of the circle circle.setFill(Color.CORNFLOWERBLUE); //Instantiating the Light.Distant class Light.Distant light = new Light.Distant(); //Setting the properties of the light source light.setAzimuth(45.0); light.setElevation(30.0); //Instantiating the Lighting class Lighting lighting = new Lighting(); //Setting the source of the light lighting.setLight(light); //Applying the lighting effect to the text text.setEffect(lighting); //Applying the lighting effect to the circle circle.setEffect(lighting); //Creating a Group object Group root = new Group(text,circle); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Distant light effect example”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls DistantLightingExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls DistantLightingExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – RadioButton

JavaFX – RadioButton ”; Previous Next A button is a component, which performs an action like submit and login when pressed. It is usually labeled with a text or an image specifying the respective action. A radio button is a type of button, which is circular in shape. It has two states, selected and deselected. The figure below shows a set of radio buttons − RadioButton in JavaFX In JavaFX, the RadioButton class represents a radio button which is a part of the package named javafx.scene.control. It is the subclass of the ToggleButton class. Action is generated whenever a radio button is pressed or released. Generally, radio buttons are grouped using toggle groups, where you can only select one of them. We can set a radio button to a group using the setToggleGroup() method. To create a radio button, use the following constructors − RadioButton() − This constructor will create radio button without any label. RadioButton(String str) − It is the parameterized constructor which constructs a radio button with the specified label text. Example Following is the JavaFX program that will create a RadioButton. Save this code in a file with the name JavafxRadiobttn.java. import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class JavafxRadiobttn extends Application { @Override public void start(Stage stage) { //Creating toggle buttons RadioButton button1 = new RadioButton(“Java”); RadioButton button2 = new RadioButton(“Python”); RadioButton button3 = new RadioButton(“C++”); //Toggle button group ToggleGroup group = new ToggleGroup(); button1.setToggleGroup(group); button2.setToggleGroup(group); button3.setToggleGroup(group); //Adding the toggle button to the pane VBox box = new VBox(5); box.setFillWidth(false); box.setPadding(new Insets(5, 5, 5, 50)); box.getChildren().addAll(button1, button2, button3); //Setting the stage Scene scene = new Scene(box, 400, 300, Color.BEIGE); stage.setTitle(“Toggled Button Example”); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved Java file from the command prompt using the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxRadiobttn.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxRadiobttn Output On executing, the above program will generate the following output − Print Page Previous Next Advertisements ”;

JavaFX – SepiaTone Effect

JavaFX – SepiaTone Effect ”; Previous Next Sepia tone effect in general changes the image from the black and white color to a reddish brown color. You can apply the Sepia Tone Effect to a node in JavaFX (image in general). The class named SepiaTone of the package javafx.scene.effect represents the sepia tone effect, this class contains two properties, which are − level − This property is of double type representing the intensity of this effect. The range of this property is 0.0 to 1.0. input − This property is of the type effect and it represents an input to the sepia tone effect. Example The following program is an example demonstrating the Sepia Tone Effect of JavaFX. In here, we are embedding the following image (tutorialspoint logo) in JavaFX scene using Image and ImageView classes. This is done at the position 100, 70 along with fit height and fit width 200 and 400 respectively. To this image, we are applying the Sepia Tone Effect with the level value 0.9. Save this code in a file with name SepiaToneEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.SepiaTone; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.stage.Stage; public class SepiaToneEffectExample extends Application { @Override public void start(Stage stage) { //Creating an image Image image = new Image(“http://www.tutorialspoint.com/images/tp-logo.gif”); //Setting the image view ImageView imageView = new ImageView(image); //Setting the position of the image imageView.setX(150); imageView.setY(0); //setting the fit height and width of the image view imageView.setFitHeight(300); imageView.setFitWidth(400); //Setting the preserve ratio of the image view imageView.setPreserveRatio(true); //Instanting the SepiaTone class SepiaTone sepiaTone = new SepiaTone(); //Setting the level of the effect sepiaTone.setLevel(0.8); //Applying SepiaTone effect to the image imageView.setEffect(sepiaTone); //Creating a Group object Group root = new Group(imageView); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Sepia tone effect example”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls SepiaToneEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls SepiaToneEffectExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Reflection Effect

JavaFX – Reflection Effect ”; Previous Next Reflection is scientifically defined as a phenomenon occurring when a light hits a surface (called a reflective surface like mirror), it gets thrown back. In such cases, all the objects present in the line of these light waves are also projected back as an image. You can also apply this reflection effect in JavaFX applications. On applying the reflection effect to a node in JavaFX, a reflection of it is added at the bottom of the node. The class named Reflection of the package javafx.scene.effect represents the reflection effect. This class contains four properties, which are − topOpacity − This property is of double type representing the top extremes opacity value of the reflection. bottomOpacity − This property is of double type representing the bottom extremes opacity value of the reflection. input − This property is of the type Effect and it represents an input to the reflection effect. topOffset − This property is of double type representing the distance between the bottom of the input and the top of the reflection. fraction −This property is of double type representing the fraction of input that is visible in the output. The range of the fraction value is 0.0 to 1.0. Example The following program is an example demonstrating the reflection effect. In here, we are drawing the text “Welcome to Tutorialspoint” filled with DARKSEAGREEN color and applying the reflection effect to it. Save this code in a file with the name ReflectionEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.Reflection; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class ReflectionEffectExample extends Application { @Override public void start(Stage stage) { //Creating a Text object Text text = new Text(); //Setting font to the text text.setFont(Font.font(null, FontWeight.BOLD, 40)); //setting the position of the text text.setX(60); text.setY(150); //Setting the text to be embedded. text.setText(“Welcome to Tutorialspoint”); //Setting the color of the text text.setFill(Color.DARKSEAGREEN); //Instanting the reflection class Reflection reflection = new Reflection(); //setting the bottom opacity of the reflection reflection.setBottomOpacity(0.0); //setting the top opacity of the reflection reflection.setTopOpacity(0.5); //setting the top offset of the reflection reflection.setTopOffset(0.0); //Setting the fraction of the reflection reflection.setFraction(0.7); //Applying reflection effect to the text text.setEffect(reflection); //Creating a Group object Group root = new Group(text); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Reflection effect example”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls ReflectionEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls ReflectionEffectExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – SplitPane

JavaFX – SplitPane ”; Previous Next The splitpane is a control with two or more slides separated by dividers. Each slide provides a different viewport allowing us to embed multiple components. We can also set the orientation of the splitpane to be horizontal or vertical. Let”s have a look at what a typical splitpane looks like − SplitPane in JavaFX In JavaFX, the class named SplitPane represents a splitpane. To use the feature of spiltpane, we need to create an instance of the SplitPane class and add the components we want to display inside it. These components can be any JavaFX nodes, such as labels, buttons, images, text fields and so on. We can use any of the below constructors to create a splitpane − SplitPane() − It is the default constructor used to create a splitpane without any predefined nodes. SplitPane(Node components) − It is the parameterized constructor of SplitPane class which will create a new splitpane with the specified nodes. Steps to create a splitpane in JavaFX To create a splitpane in JavaFX, follow the steps given below. Step 1: Create two or more Nodes As discussed earlier, a splitpane contains at least two slides. Hence, our first step would be creating two or more nodes to display within these distinct slides. For the sake of this example, we are going to use the labels. In JavaFX, the labels are created by instantiating the class named Label which belongs to a package javafx.scene.control. Create the labels as shown below − // Creating four labels Label labelOne = new Label(“Label 1”); Label labelTwo = new Label(“Label 2”); Label labelThree = new Label(“Label 3”); Label labelFour = new Label(“Label 4”); Similarly, create the desired nodes by instantiating their respective classes. Step 2: Instantiate the SplitPane class Instantiate the SplitPane class of package javafx.scene.control without passing any parameter value to its constructor and add all the labels to the splitpane using the getItems() method. // instantiating the SplitPane class SplitPane splitP = new SplitPane(); // adding the labels to the SplitPane splitP.getItems().addAll(labelOne, labelTwo, labelThree, labelFour); Step 3: Launching Application After creating the SplitPane and adding the labels to it, follow the given steps below to launch the application properly − Firstly, instantiate the class named Scene by passing the SplitPane object as a parameter value to its constructor. Also, pass dimensions of the application screen as optional parameters to this constructor. Then, set the title to the stage using the setTitle() method of the Stage class. Now, a Scene object is added to the stage using the setScene() method of the class named Stage. Display the contents of the scene using the method named show(). Lastly, the application is launched with the help of the launch() method. Example Following is the program which will create a splitpane in JavaFX. Save this code in a file with the name NewSplitpane.java. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.SplitPane; import javafx.stage.Stage; public class NewSplitpane extends Application { @Override public void start(Stage stage) { // Creating four labels Label labelOne = new Label(“Label 1”); Label labelTwo = new Label(“Label 2”); Label labelThree = new Label(“Label 3”); Label labelFour = new Label(“Label 4”); // instantiating the SplitPane class SplitPane splitP = new SplitPane(); // adding the labels to the SplitPane splitP.getItems().addAll(labelOne, labelTwo, labelThree, labelFour); // Creating a Scene with the SplitPane as its root node Scene scene = new Scene(splitP, 400, 300); // to set the title stage.setTitle(“SplitPane in JavaFX”); // Setting the Scene of the Stage stage.setScene(scene); // Display the Stage stage.show(); } public static void main(String[] args) { launch(args); } } Compile and execute the above Java file using the command prompt with the help of following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls NewSplitpane.java java –module-path %PATH_TO_FX% –add-modules javafx.controls NewSplitpane Output On executing, the above program generates a JavaFX window displaying a SplitPane with four labels and three dividers as shown below. Setting the Orientation of the SplitPane By default, the SplitPane has a horizontal orientation, meaning that the components are placed next to each other from left to right. We can change the orientation to vertical by calling the setOrientation() method of the SplitPane class and passing an Orientation.VERTICAL argument. Example In the following JavaFX program, we will create a vertical SplitPane. Save this code in a file with the name SplitpaneDemo.java. import javafx.application.Application; import javafx.geometry.Orientation; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.SplitPane; import javafx.stage.Stage; import javafx.scene.layout.VBox; public class SplitpaneDemo extends Application { @Override public void start(Stage stage) { // Create a SplitPane with vertical orientation SplitPane splitP = new SplitPane(); splitP.setOrientation(Orientation.VERTICAL); // vertical box to hold the labels VBox box1 = new VBox(); VBox box2 = new VBox(); // Create two labels and add them to the SplitPane box1.getChildren().add(new Label(“This is nthe nfirst section”)); box2.getChildren().add(new Label(“This is nthe nsecond section”)); splitP.getItems().addAll(box1, box2); // Set the divider position to 50 splitP.setDividerPositions(0.5); // Create a scene and show the stage Scene scene = new Scene(splitP, 400, 300); stage.setTitle(“SplitPane in JavaFX”); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } Compile and execute the saved Java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls SplitpaneDemo.java java –module-path %PATH_TO_FX% –add-modules javafx.controls SplitpaneDemo Output When we execute the above code, it will generate the following output. Print Page Previous Next Advertisements ”;

JavaFX – Bloom Effect

JavaFX – Bloom Effect ”; Previous Next The Bloom effect in JavaFX will make the pixels in some portions of the node are made to glow. The class named Bloom of the package javafx.scene.effect represents the bloom effect. This class contains two properties, which are − input − This property is of the type Effect and it represents an input to the bloom effect. threshold − This property is of the type double; this represents a threshold value of luminosity of the pixels of the node. All those pixels having luminosity greater than equal to this value are made to glow. The range of the threshold value is 0.0 to 1.0. Example Following is an example demonstrating the bloom effect. We will be drawing a text “Welcome to Tutorialspoint” and applying the bloom effect to it with a threshold value 1.0. Save this code in a file with the name BloomEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.Bloom; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class BloomEffectExample extends Application { @Override public void start(Stage stage) { //Creating a Text object Text text = new Text(); //Setting font to the text text.setFont(Font.font(null, FontWeight.BOLD, 40)); //setting the position of the text text.setX(60); text.setY(150); //Setting the text to be embedded. text.setText(“Welcome to Tutorialspoint”); //Setting the color of the text text.setFill(Color.DARKSEAGREEN); //Instantiating the Rectangle class Rectangle rectangle = new Rectangle(); //Setting the position of the rectangle rectangle.setX(50.0f); rectangle.setY(80.0f); //Setting the width of the rectangle rectangle.setWidth(500.0f); //Setting the height of the rectangle rectangle.setHeight(120.0f); //Setting the color of the rectangle rectangle.setFill(Color.TEAL); //Instantiating the Bloom class Bloom bloom = new Bloom(); //setting threshold for bloom bloom.setThreshold(0.1); //Applying bloom effect to text text.setEffect(bloom); //Creating a Group object Group root = new Group(rectangle, text); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle(“Sample Application”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls BloomEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls BloomEffectExample Output On executing, the above program generates a JavaFX window as shown below. Print Page Previous Next Advertisements ”;

JavaFX – Label

JavaFX – Label ”; Previous Next A Label is a piece of text that describe or informs users about the functionality of other elements in the application. It helps in reducing confusion and provides clarity which leads to a better user experience. Always remember, it is a not an editable text control. In the figure below, we can see a button in red box and there are some text describing its purpose − Label in JavaFX In JavaFX, the label is represented by a class named Label which belongs to the javafx.scene.control package. To create a label in JavaFX application, we can use any of the below constructor − Label() − It is the default constructor that constructs an empty label. Label(String str) − It constructs a label with the predefined text. Label(String str, Node graph) − It constructs a new label with the specified text and graph. Steps to create a Label in JavaFX To create a Label in JavaFX, follow the steps given below − Step 1: Instantiate the Label class As discussed earlier, we need to instantiate the Label class to create a label text. We can use either its default constructor or parameterized constructor. If we use the default one, the label text is added by using the setText() method. // Instanting the Label class Label label = new Label(“Sample label”); Step 2: Set the required properties of Label Just like a text node we can set the desired properties like font and font color to the label node in JavaFX using the setFont() method and setFill() method respectively. // Setting font to the label Font font = Font.font(“Brush Script MT”, FontWeight.BOLD, FontPosture.REGULAR, 25); label.setFont(font); // Filling color to the label label.setTextFill(Color.BROWN); Step 4: Launching Application Once the Label is created and its properties are set, define a group object to hold the label. Next, create a Scene object by passing the group obejct and the dimensions of the Scene to its constructor. Then, set the stage and launch the application to display the result. Example In the following example, we are going to create a Label in JavaFX application. Save this code in a file with the name JavafxLabel.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.stage.Stage; public class JavafxLabel extends Application { public void start(Stage stage) { //Creating a Label Label label = new Label(“Sample label”); //Setting font to the label Font font = Font.font(“Brush Script MT”, FontWeight.BOLD, FontPosture.REGULAR, 25); label.setFont(font); //Filling color to the label label.setTextFill(Color.BROWN); //Setting the position label.setTranslateX(150); label.setTranslateY(25); Group root = new Group(); root.getChildren().add(label); //Setting the stage Scene scene = new Scene(root, 400, 300, Color.BEIGE); stage.setTitle(“Label Example”); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } } To compile and execute the saved Java file from the command prompt, use the following commands − javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxLabel.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxLabel Output When we execute the above code, it will generate a Label text as shown in the following output. Print Page Previous Next Advertisements ”;

JavaFX – Blend Effect

JavaFX – Blend Effect ”; Previous Next In general, blend means mixture of two or more different things or substances. If we apply the blend effect, it will take the pixels of two different inputs. This will be done at the same location and it produces a combined output based on the blend mode. For example, if we draw two objects the top object covers the bottom one. On applying the blend effect, the pixels of the two objects in the overlap area are combined and displayed based on the input mode. The class named Blend of the package javafx.scene.effect represents the blend effect, this class contains four properties, which are − bottomInput − This property is of the type Effect and it represents the bottom input to the blend effect. topInput − This property is of the type Effect and it represents the top input to the blend effect. opacity − This property is of double type and it represents the opacity value modulated with the top input. mode − This property is of the type BlendMode and it represents the mode used to blend the two inputs together. Example Following is an example demonstrating the blend effect. In here, we are drawing a circle filled with BROWN color, on top of it lies a BLUEVIOLET ColorInput. We have applied the blend effect choosing a multiply mode In the overlap area, the colors of the two objects were multiplied and displayed. Save this code in a file with the name BlendEffectExample.java. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.Circle; import javafx.scene.effect.Blend; import javafx.scene.effect.BlendMode; import javafx.scene.effect.ColorInput; import javafx.scene.paint.Color; public class BlendEffectExample extends Application { @Override public void start(Stage stage) { //Drawing a Circle Circle circle = new Circle(); //Setting the center of the Circle circle.setCenterX(75.0f); circle.setCenterY(75.0f); //Setting radius of the circle circle.setRadius(30.0f); //Setting the fill color of the circle circle.setFill(Color.BROWN); //Instantiating the blend class Blend blend = new Blend(); //Preparing the to input object ColorInput topInput = new ColorInput(35, 30, 75, 40, Color.BLUEVIOLET); //setting the top input to the blend object blend.setTopInput(topInput); //setting the blend mode blend.setMode(BlendMode.SRC_OVER); //Applying the blend effect to circle circle.setEffect(blend); //Creating a Group object Group root = new Group(circle); //Creating a scene object Scene scene = new Scene(root, 150, 150); //Setting title to the Stage stage.setTitle(“Blend Example”); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls BlendEffectExample.java java –module-path %PATH_TO_FX% –add-modules javafx.controls BlendEffectExample Output On executing, the above program generates a JavaFX window as shown below. Blend Modes S.NO Mode & Description Output 1 ADD In this mode, the color values of the top and bottom inputs are added and displayed. 2 MULTIPLY In this mode, the color values of the top and bottom inputs are multiplied and displayed. 3 DIFFERENCE In this mode, among the color values of the top and bottom inputs, the darker one is subtracted from the lighter one and displayed. 4 RED In this mode, the red components of the bottom input were replaced by the red components of the top input. 5 BLUE In this mode, the blue components of the bottom input were replaced by the blue components of the top input. 6 GREEN In this mode, the green components of the bottom input were replaced by the green components of the top input. 7 EXCLUSION In this mode, the color components of the two inputs were multiplied and doubled. Then they are subtracted from the sum of the color components of the bottom input. The resultant is then displayed. 8 COLOR_BURN In this mode, the inverse of the bottom input color component was divided by the top input color component. Thus, the obtained value is inverted and displayed. 9 COLOR_DODGE In this mode, the bottom input color components were divided by the inverse of the top input color components and thus obtained value is inverted and displayed. 10 LIGHTEN In this mode, the lighter color component, among the both inputs are displayed. 11 DARKEN In this mode, the darker color component, among the top and bottom inputs is displayed. 12 SCREEN In this mode, the color components of the top and bottom inputs were inverted, multiplied and thus obtained value is inverted and displayed. 13 OVERLAY In this mode, based on the bottom input color, the color components of the two input values were multiplied or screened and the resultant is displayed. 14 HARD_LIGHT In this mode, based on the top input color, the color components of the two input values were multiplied or screened and the resultant is displayed. 15 SOFT_LIGH In this mode, based on the top input color, the color components of the two input values were softened or lightened and the resultant is displayed. 16 SRC_ATOP In this mode, the over lapping area is filled with the color component of the bottom input. While the nonoverlapping area is filled with the color component of the top input. 17 SRC_OVER In this mode, the top input is drawn over the bottom input. Print Page Previous Next Advertisements ”;

JavaFX – FileChooser

JavaFX – FileChooser ”; Previous Next A file chooser is a graphical user interface elements that allow users to browse through file system. Generally, it is used to open and save either single or multiple files. In the below figure, we can see a file chooser in google drive application − FileChooser in JavaFX In JavaFX, the file chooser is represented by a class named FileChooser which belongs to a package named javafx.scene.control. We can create a file chooser component within our JavaFX application by instantiating this class. This class has only one constructor, i.e. its default constructor. However, it provides multiple properties, which are listed below − initialDirectory − This property specifies the initial directory of the file chooser. You can set value to it using the setInitialDirectory() method. selectedExtensionFilter − This property specifies the extension filter displayed in the dialog. You can set value to it using the setSelectedExtensionFilter() method. Title − The property specifies the title of the dialog. can set value to it using the setTitle() method. How to create a FileChooser in JavaFX? Follow the steps given below to create a file chooser in JavaFX. Step 1: Create a node to associate the FileChooser A file chooser must be associated with another node, like menu or button, so that when the node is clicked, a dialog window opens for file selection. For this purpose, we have used a Menu as shown in the below code − //Creating a menu Menu fileMenu = new Menu(“File”); //Creating menu Items MenuItem item = new MenuItem(“Open Image”); Step 2: Instantiate the FileChooser class To create a file chooser, instantiate the FileChooser class. Then, set the desired file extension with the help of getExtensionFilters() method as shown in the following code block − //Creating a File chooser FileChooser fileChooser = new FileChooser(); fileChooser.setTitle(“Open Image”); fileChooser.getExtensionFilters().addAll(new ExtensionFilter(“All Files”, “*.*”)); Step 3: Add action handler to the Menu It is important to set an action to the menu as it will trigger the opening of the file chooser dialog. //Adding action on the menu item item.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { //Opening a dialog box fileChooser.showOpenDialog(stage); }}); Step 4: Launch the Application Once the file chooser is created and its properties are set, create a MenuBar. Next, pass the Menubar object to the constructor of Group class. Then, set the Scene and Stage. Lastly, launch the application with the help of the launch() method. Example Following is the program that will create a FileChooser using JavaFX. Save this code in a file with the name JavafxFilechooser.java. import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.paint.Color; import javafx.stage.FileChooser; import javafx.stage.Stage; import javafx.stage.FileChooser.ExtensionFilter; public class JavafxFilechooser extends Application { public void start(Stage stage) { //Creating a menu Menu fileMenu = new Menu(“File”); //Creating menu Items MenuItem item = new MenuItem(“Open Image”); fileMenu.getItems().addAll(item); //Creating a File chooser FileChooser fileChooser = new FileChooser(); fileChooser.setTitle(“Open Image”); fileChooser.getExtensionFilters().addAll(new ExtensionFilter(“All Files”, “*.*”)); //Adding action on the menu item item.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { //Opening a dialog box fileChooser.showOpenDialog(stage); }}); //Creating a menu bar and adding menu to it. MenuBar menuBar = new MenuBar(fileMenu); menuBar.setTranslateX(3); menuBar.setTranslateY(3); //Setting the stage Group root = new Group(menuBar); Scene scene = new Scene(root, 400, 300, Color.BEIGE); stage.setTitle(“File Chooser Example”); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } } Compile and execute the saved Java file from the command prompt using the following commands. javac –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxFilechooser.java java –module-path %PATH_TO_FX% –add-modules javafx.controls JavafxFilechooser Output On executing, the above program displays a Button. When we click that button, it will show a pop-up window allowing the user to choose a file. Print Page Previous Next Advertisements ”;