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 container
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.canvas.*;
import
javafx.scene.web.*;
import
javafx.scene.layout.*;
import
javafx.scene.image.*;
import
java.io.*;
import
javafx.geometry.*;
import
javafx.scene.Group;
import
javafx.scene.paint.*;
public
class
Background_2
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"creating Background"
);
// create a label
Label label =
new
Label(
"Name : "
);
// create a text field
TextField textfield =
new
TextField();
// set preferred column count
textfield.setPrefColumnCount(
10
);
// create a button
Button button =
new
Button(
"OK"
);
// add the label, text field and button
HBox hbox =
new
HBox(label, textfield, button);
// set spacing
hbox.setSpacing(
10
);
// set alignment for the HBox
hbox.setAlignment(Pos.CENTER);
// create a scene
Scene scene =
new
Scene(hbox,
280
,
280
);
// create a background fill
BackgroundFill background_fill =
new
BackgroundFill(Color.PINK,
CornerRadii.EMPTY, Insets.EMPTY);
// create Background
Background background =
new
Background(background_fill);
// set background
hbox.setBackground(background);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(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 container
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.canvas.*;
import
javafx.scene.web.*;
import
javafx.scene.layout.*;
import
javafx.scene.image.*;
import
java.io.*;
import
javafx.geometry.*;
import
javafx.scene.Group;
public
class
Background_1
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"creating Background"
);
// create a label
Label label =
new
Label(
"Name : "
);
// create a text field
TextField textfield =
new
TextField();
// set preferred column count
textfield.setPrefColumnCount(
10
);
// create a button
Button button =
new
Button(
"OK"
);
// add the label, text field and button
HBox hbox =
new
HBox(label, textfield, button);
// set spacing
hbox.setSpacing(
10
);
// set alignment for the HBox
hbox.setAlignment(Pos.CENTER);
// create a scene
Scene scene =
new
Scene(hbox,
280
,
280
);
// create a input stream
FileInputStream input =
new
FileInputStream(
"f:\\gfg.png"
);
// create a image
Image image =
new
Image(input);
// create a background image
BackgroundImage backgroundimage =
new
BackgroundImage(image,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
// create Background
Background background =
new
Background(backgroundimage);
// set background
hbox.setBackground(background);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(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