Reputation: 83
here is my code:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class NotesApplication extends Application {
@Override
public void start(Stage stage) {
StackPane root = new StackPane();
root.setPrefSize(700,700);
StackPane leftPane = new StackPane();
leftPane.setPrefSize(100, 700);
Button button = new Button("Button 1");
leftPane.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));
leftPane.getChildren().add(button);
leftPane.setAlignment(Pos.CENTER_LEFT);
StackPane rightPane = new StackPane();
rightPane.setPrefSize(200,700);
Button button1 = new Button("Button 2");
rightPane.getChildren().add(button1);
rightPane.setAlignment(Pos.CENTER_RIGHT);
root.getChildren().addAll(leftPane, rightPane);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
As you can see, I have a class with a root StackPane and with another two StackPanes(leftPane and rightPane) inside the root StackPane. I want to set the background color of only the leftPane to yellow but the result is that the whole window gets the yellow background, what am I doing wrong?
Thanks in advance.
Upvotes: 0
Views: 357
Reputation: 939
If you don't have a particular reason to use StackPane as root, you could use AnchorPane. You could set 3 side anchors for your left and right StackPanes and bind your Stackpanes's width with your roots width, so if you resize your window your stackpanes going to adapt to it
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class NotesApplication extends Application {
@Override
public void start(Stage stage) {
AnchorPane root = new AnchorPane(); // Changed stackpane to anchor pane
root.setPrefSize(700,700);
StackPane leftPane = new StackPane();
leftPane.minWidthProperty().bind(root.widthProperty().divide(2)); // Binding leftPane's width with the half of your root
Button button = new Button("Button 1");
leftPane.setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));
leftPane.getChildren().add(button);
leftPane.setAlignment(Pos.CENTER_LEFT);
StackPane rightPane = new StackPane();
rightPane.minWidthProperty().bind(root.widthProperty().divide(2)); // Binding rightPane's width with the half of your root
Button button1 = new Button("Button 2");
rightPane.getChildren().add(button1);
rightPane.setAlignment(Pos.CENTER_RIGHT);
rightPane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
root.getChildren().addAll(leftPane, rightPane);
// Setting the anchors
root.setLeftAnchor(leftPane, 0.0);
root.setTopAnchor(leftPane, 0.0);
root.setBottomAnchor(leftPane, 0.0);
root.setRightAnchor(rightPane, 0.0);
root.setTopAnchor(rightPane, 0.0);
root.setBottomAnchor(rightPane, 0.0);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 1