I want to change the color of a Pane
which I get as a String
from user.
How can I set this String
as a background color in my pane?
Code:
colorField.setOnKeyTyped(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
color = colorField.getText();
}
});
Set Background Color You can set a background color for a JavaFX Region like this: Pane pane = new Pane(); BackgroundFill backgroundFill = new BackgroundFill( Color. valueOf("#ff00ff"), new CornerRadii(10), new Insets(10) ); Background background = new Background(backgroundFill); pane. setBackground(background);
The simplest way to set the JavaFX Scene background color or image is by invoking the Scene 's setFill() method, which can accept a color, gradient or image pattern. A more flexible way to set the background of a scene is to set the root node's background, which can accept multiple images and fills.
Pane provides properties for setting the size range directly. These properties default to the sentinel value Region.
A Pane is a UI element ("Node") that contains other UI elements ("child nodes") and manages the layout of those nodes within the Pane . There are several predefined Pane types (subclasses of Pane ) that differ in how they lay out their child nodes.
If you really just want to know how to accomplish that particular thing, I'd suggest the following:
Set the Nodes' CSS like this, using the hexacolor that was entered by the user:
String enteredByUser = "abcdef";
yournode.setStyle("-fx-background-color: #" + enteredByUser);
If you want to know more please be more specific with you questions and provide some code samples.
Since you tagged this question with 'javafx-8' i'll provide that code example as well(only works in javafx 8):
yournode.setBackground(new Background(new BackgroundFill(Color.web("#" + enteredByUser), CornerRadii.EMPTY, Insets.EMPTY)));
Hope it helps, Laurenz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With