JavaFX | Background Class

Background class is a part of JavaFX. Background class sets the background of a region. Every background is composed of several fills or background images but cannot be null but it may be empty. Background class is immutable, so you can freely reuse the same Background on many different Regions.
Constructors of the class:
- Background(BackgroundFill… f): Creates a new background object with specified fills.
- Background(BackgroundFill[] fills, BackgroundImage[] images): Creates a new background object with specified fills and background image.
- Background(BackgroundImage… i): Creates a new background object with specified background images.
- Background(List fills, List images): Creates a new background object with list of specified fills and background images.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getFills() | Returns the list of all the fills of a background. |
| getImages() | Returns the list of all the background images of a background. |
| getOutsets() | Returns outsets of this Background. |
| isEmpty() | Returns whether the background is empty. |
| isFillPercentageBased() | Returns whether the fill of this Background is based on percentages. |
Below programs illustrate the use of Background class:
- Java program to set a fill for the background of a container: In this program we will create a Background named background with specified BackgroundFill and add this to the background. We will create an HBox named hbox, a Label named label, TextField named textfield and a Button named button . Now add the label, textfield and button to the HBox. We will set the background of hbox using the setBackground() function.Now set the alignment of HBox to Pos.CENTER and also add some spacing between the nodes using setSpacing() method. We will create a Scene named scene and add the HBox to the scene. The scene will be set to the stage using the setScene() function. We will call the show() function to display the results.
// Java program to set a fill for the background// of a containerimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.layout.*;importjavafx.scene.image.*;importjava.io.*;importjavafx.geometry.*;importjavafx.scene.Group;importjavafx.scene.paint.*;publicclassBackground_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("creating Background");// create a labelLabel label =newLabel("Name : ");// create a text fieldTextField textfield =newTextField();// set preferred column counttextfield.setPrefColumnCount(10);// create a buttonButton button =newButton("OK");// add the label, text field and buttonHBox hbox =newHBox(label, textfield, button);// set spacinghbox.setSpacing(10);// set alignment for the HBoxhbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(hbox,280,280);// create a background fillBackgroundFill background_fill =newBackgroundFill(Color.PINK,CornerRadii.EMPTY, Insets.EMPTY);// create BackgroundBackground background =newBackground(background_fill);// set backgroundhbox.setBackground(background);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Java program to add an image to the background of a container: In this program we will create a Background named background with specified BackgroundImage and add this image to the background of the container. Import the image using the FileInputStream and then convert the file into Image object Use this Image object to create a BackgroundImage. We will create an HBox named hbox, a Label named label, TextField named textfield and a Button named button . Now add the label, textfield, and button to the HBox. Set the background of the hbox using the setBackground() function. Set the alignment of HBox to Pos.CENTER and also add some spacing between the nodes using setSpacing() method. We will create a Scene named scene and add the HBox to the scene. The scene will be set to the stage using the setScene() function. Finally call the show() method to display the result.
// Java program to add an image to// the background of a containerimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.layout.*;importjavafx.scene.image.*;importjava.io.*;importjavafx.geometry.*;importjavafx.scene.Group;publicclassBackground_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("creating Background");// create a labelLabel label =newLabel("Name : ");// create a text fieldTextField textfield =newTextField();// set preferred column counttextfield.setPrefColumnCount(10);// create a buttonButton button =newButton("OK");// add the label, text field and buttonHBox hbox =newHBox(label, textfield, button);// set spacinghbox.setSpacing(10);// set alignment for the HBoxhbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(hbox,280,280);// create a input streamFileInputStream input =newFileInputStream("f:\\gfg.png");// create a imageImage image =newImage(input);// create a background imageBackgroundImage backgroundimage =newBackgroundImage(image,BackgroundRepeat.NO_REPEAT,BackgroundRepeat.NO_REPEAT,BackgroundPosition.DEFAULT,BackgroundSize.DEFAULT);// create BackgroundBackground background =newBackground(backgroundimage);// set backgroundhbox.setBackground(background);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Background.html




