I'm learning javafx and I have a problem.
I'm building an interface with a border pane and a menu bar at the top and when I click on the items I want scenes to be loaded on the center of the border pane. That seems to be working alright. I want to add a button to close the scene but I can't make it work.
See below the code.
stage = primaryStage;
stage.setTitle("My Program");
BorderPane pane = new BorderPane();
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("File");
MenuItem load = new MenuItem("Load");
MenuItem save = new MenuItem("Save");
....
BorderPane sp = new BorderPane();
sp.setStyle("-fx-background: #FF0000;");
Button btn = new Button("Close");
btn.setPrefSize(200, 20);
btn.setLayoutX(200);
btn.setLayoutY(200);
sp.getChildren().add(btn);
load.setOnAction(e -> pane.setCenter(sp));
btn.setOnAction(e -> ???????????????????);
scene1 = new Scene(sp);
scene = new Scene(pane, 800, 500);
stage.setScene(scene);
stage.show();
I was wondering if it's something that I can do and what code I should put instead of the question marks.
Any help is appreciated. Thanks in advance.
btn.setOnAction(e -> pane.setCenter(null));
to clear the border pane content, or
btn.setOnAction(e -> stage.hide());
to close the entire window
Related
Is it possible to make Button over the View?
I have something like
private BorderPane makePane(Node view) {
BorderPane pane = new BorderPane();
Button leftButton = new Button();
Button rightButton = new Button();
Button bottomButton = new Button();
leftButton.setOnAction(e -> goBack(pane));
rightButton.setOnAction(e -> goNext(pane));
bottomButton.setOnAction(e -> play());
pane.setCenter(view);
pane.setLeft(leftButton);
pane.setRight(rightButton);
pane.setBottom(bottomButton);
return pane;
}
Now, I have view on the centter and buttons on the sides. But it crops my Image or Video, depends on which instance of Node I get.
There is crop on the pane from left and right sides for each button. I don't want to see the white/black/other vertical line through whole window only for 1 button.
I wanna it will be okay when the number variables is changed, but when the are increased the button goes out from the window. How to fix it? Also how to put the bar down to the level of "10$", so they will be in the same row?
Before :
After :
Here is my code :
VBox vboxBottom = new VBox();
HBox hboxBottomElements = new HBox(15);
HBox hboxBottomMain = new HBox(0);
Region region = new Region();
region.setPrefWidth(500);
hboxBottomElements.getChildren().addAll(visaLabel, separator2, adLabel, separator3, governRelationStatus, separator4, region, next);
hboxBottomElements.setPadding(new Insets(5));
vboxBottom.getChildren().addAll(separator1, new Group(hboxBottomElements));
vboxBottom.setPadding(new Insets(3));
hboxBottomMain.getChildren().addAll(new Group(moneyBox), vboxBottom);
hboxBottomMain.setPadding(new Insets(3));
layout.setBottom(hboxBottomMain);
By using a Group here
vboxBottom.getChildren().addAll(separator1, new Group(hboxBottomElements));
you're creating a layout structure that resizes hboxBottomElements to it's prefered size independent of the space available.
HBox simply moves elements out the right side of it's bounds, if the space available does not suffice. This means if the Group containing moneyBox grows, the Button is moved out of the HBox...
The following simpler example demonstrates the behavior:
#Override
public void start(Stage primaryStage) {
Button btn = new Button("Do something");
HBox.setHgrow(btn, Priority.NEVER);
btn.setMinWidth(Region.USE_PREF_SIZE);
Region filler = new Region();
filler.setPrefWidth(100);
HBox.setHgrow(filler, Priority.ALWAYS);
Rectangle rect = new Rectangle(200, 50);
HBox hBox = new HBox(rect, filler, btn);
Scene scene = new Scene(hBox);
primaryStage.setScene(scene);
primaryStage.show();
}
This will resize filler to make the HBox fit the window.
Now replace
Scene scene = new Scene(hBox);
with
Scene scene = new Scene(new Group(hBox));
and the Button will be moved out of the window...
I've been trying to make a toolbar inside a window with a checkers game, what happens now is, Checkers game opening in a separate window and so is the toolbar, what am I doing wrong? How can I make this code open in one window with both functions?
#Override
public void start(Stage primaryStage) throws Exception {
Stage toolStage = new Stage();
Button btnNewGame = new Button("New Game");
Button btnConcede = new Button("Concede");
Button btnNetwork = new Button("Network");
ToolBar toolBar = new ToolBar();
toolBar.getItems().addAll( new Separator(), btnNewGame, btnConcede, btnNetwork);
BorderPane pane = new BorderPane();
pane.setTop(toolBar);
Scene toolScene = new Scene(pane, 600, 400);
toolStage.setScene(toolScene);
toolStage.show();
Scene scene = new Scene(createContent());
primaryStage.setTitle("Dam spill - OBJ2000 Eksamen 2016");
primaryStage.setScene(scene);
primaryStage.show();
}
You're creating a new Scene+Stage fot the toolbar, show it and then show the content in the primaryStage instead of adding both toolbar and content as parts of the same scene, e.g. by adding the content as center node of the BorderPane:
#Override
public void start(Stage primaryStage) throws Exception {
Button btnNewGame = new Button("New Game");
Button btnConcede = new Button("Concede");
Button btnNetwork = new Button("Network");
ToolBar toolBar = new ToolBar();
toolBar.getItems().addAll( new Separator(), btnNewGame, btnConcede, btnNetwork);
BorderPane pane = new BorderPane();
pane.setTop(toolBar);
pane.setCenter(createContent());
Scene scene = new Scene(pane, 600, 400);
primaryStage.setTitle("Dam spill - OBJ2000 Eksamen 2016");
primaryStage.setScene(scene);
primaryStage.show();
}
My code creates a window and lays it out exactly how I want... initially. However, if I maximise the window, the top and bottom parts of the border pane do not remain in the centre. They drift off to the top left and bottom left corners.
I tried to disable the maximise window option, but again it messes up the look of the page, with the top and bottom parts moving.
Here is my code:
#Override
public void start(Stage startWindow) {
startWindow.setTitle("QuizApp");
BorderPane borderPane = new BorderPane();
borderPane.setTop(addHorizontalBoxWithMessage());
borderPane.setCenter(addImageView());
borderPane.setBottom(addHorizontalBoxWithButton());
Scene scene = new Scene(borderPane, 750, 663);
startWindow.setScene(scene);
scene.getStylesheets().add(StartWindow.class.getResource("application.css").toExternalForm());
// startWindow.resizableProperty().setValue(Boolean.FALSE);
startWindow.show();
}
public HBox addHorizontalBoxWithMessage() {
HBox hBox = new HBox();
hBox.setId("hBox");
hBox.setMinWidth(750);
hBox.setMinHeight(50);
hBox.setMaxWidth(750);
hBox.setMaxHeight(50);
hBox.setPadding(new Insets(0, 10, 0, 10));
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Text message = new Text("Welcome to the QuizApp!");
message.setId("message");
hBox.getChildren().add(message);
return hBox;
}
public ImageView addImageView() {
Image image = new Image(getClass().getResourceAsStream("quiz.jpg"));
ImageView imageView = new ImageView();
imageView.setImage(image);
imageView.setFitWidth(750);
imageView.setFitHeight(563);
return imageView;
}
public HBox addHorizontalBoxWithButton() {
HBox hBox = new HBox();
hBox.setId("hBox");
hBox.setMinWidth(750);
hBox.setMinHeight(50);
hBox.setMaxWidth(750);
hBox.setMaxHeight(50);
hBox.setPadding(new Insets(10, 10, 10, 10));
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button registerButton = new Button("Register");
registerButton.setPrefSize(100, 30);
Button loginButton = new Button("Login");
loginButton.setPrefSize(100, 30);
hBox.getChildren().add(registerButton);
hBox.getChildren().add(loginButton);
return hBox;
}
I only started teaching myself JavaFX last night but can't seem to figure out where I am going wrong, or find a solution to my problem.
Thanks in advance for any advice.
In your code replace : hBox.setMaxWidth(750);
with:
hBox.setMaxWidth(Region.USE_COMPUTED_SIZE);
The problem was that after resizing your hbox, the width was still 750 px long
Another easy option is to use JavaFX Scene Builder to figure out how GUI components works.
Ok so I am in a basic programming class, and we have to make a borderpane that has all the different panes inside of it. When i try to put just a regular pane in the center with a "hello" button inside it the button gets put on the top the borderpane. All of the other panes I use a method that makes 5 buttons and randomly inserts a word and randomly rotates it. How do i get the pane to be in the center and not the top
StackPane root = new StackPane();
Button[] buttons = new Button[5];
buttons = createButtons();
root.getChildren().addAll(buttons);
FlowPane flow = new FlowPane();
Button[] buttons1 = new Button[5];
buttons1 = createButtons();
flow.getChildren().addAll(buttons1);
HBox hbox= new HBox();
Button[] buttons2 = new Button[5];
buttons2 = createButtons();
hbox.getChildren().addAll(buttons2);
VBox vbox = new VBox();
Button[] buttons3 = new Button[5];
buttons3 = createButtons();
vbox.getChildren().addAll(buttons3);
Pane pane = new Pane();
Button btn = new Button();
btn.setText("Hello");
pane.getChildren().add(btn);
BorderPane border = new BorderPane();
border.setBottom(hbox);
border.setLeft(root);
border.setRight(vbox);
border.setTop(flow);
border.setCenter(pane);
Scene scene = new Scene(border, 500, 500);
primaryStage.setTitle("Buttons");
primaryStage.setScene(scene);
primaryStage.show();
}
You should add a background color for identification, e. g. via setStyle("-fx-background-color:black). This way you see how each of your panes are laid out.
Example:
public class BindIt extends Application {
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
root.setStyle("-fx-background-color:red");
Button[] buttons = new Button[5];
buttons = createButtons();
root.getChildren().addAll(buttons);
FlowPane flow = new FlowPane();
flow.setStyle("-fx-background-color:blue");
Button[] buttons1 = new Button[5];
buttons1 = createButtons();
flow.getChildren().addAll(buttons1);
HBox hbox= new HBox();
hbox.setStyle("-fx-background-color:green");
Button[] buttons2 = new Button[5];
buttons2 = createButtons();
hbox.getChildren().addAll(buttons2);
VBox vbox = new VBox();
vbox.setStyle("-fx-background-color:yellow");
Button[] buttons3 = new Button[5];
buttons3 = createButtons();
vbox.getChildren().addAll(buttons3);
Pane pane = new Pane();
pane.setStyle("-fx-background-color:cyan");
Button btn = new Button();
btn.setText("Hello");
pane.getChildren().add(btn);
BorderPane border = new BorderPane();
border.setStyle("-fx-background-color:black");
border.setBottom(hbox);
border.setLeft(root);
border.setRight(vbox);
border.setTop(flow);
border.setCenter(pane);
Scene scene = new Scene(border, 500, 500);
primaryStage.setTitle("Buttons");
primaryStage.setScene(scene);
primaryStage.show();
}
private Button[] createButtons() {
return new Button[] { new Button( "Button") };
}
public static void main(String[] args) {
launch(args);
}
}
It gives you this:
Now you see that the center pane is in the center and fills the center. The problem is that the button isn't in the center. The easy solution is to just use a StackPane for the center pane:
StackPane pane = new StackPane();
Another solution would be to relocate the button to the center of the center pane. But then you'd have to consider resizing of the parent.