I am using JavaFX 2 in Netbeans. How do I get the width and height of an element to adjust when the Frame is resized? Here is my layout:
Stage stage = new Stage();
stage.setTitle("Hello World");
final Group root = new Group();
Scene scene = new Scene(root);
BorderPane border = new BorderPane();
border.setPrefWidth(stage.getWidth());
border.setPrefHeight(stage.getHeight());
HBox outerHBox = new HBox();
border.setCenter(outerHBox);
root.getChildren().add(border);
stage.setScene(scene);
After some more research
I found where this has been done before http://java.dzone.com/articles/setting-stage-javafx-sdk but it is in an older FX (very different from JavaFX 2). I am having trouble translating it. Looks like I should be using binding? I've never used binding before and I've barely used FX.
Whats the best way to accomplish this?
Not all Node classes enable resizing. The Group class is one of these. You'll understand this when calling isResizable() on your root object. Use instead a subclass of Region e.g. BorderPane as your root.
Stage stage = new Stage();
stage.setTitle("Hello World");
final BorderPane border = new BorderPane();
Scene scene = new Scene(border);
Button button = new Button("test");
HBox outerHBox = new HBox();
outerHBox.getChildren().add(button);
outerHBox.setAlignment(Pos.CENTER);
border.setCenter(outerHBox);
stage.setScene(scene);
Your example should work, now.
Related
How to make such a settings panel so that you can see the scene with 3d figures as in the picture?
my scene is under the panel.
I create a BorderPane,
And she completely closes the scene.
My code is in the second picture, but I don’t know how to make the scene visible
RadioButton rdoEasy = new RadioButton("Easy");
RadioButton rdoMedium = new RadioButton("Medium");
RadioButton rdoHard = new RadioButton("Hard");
var groupDifficulty = new ToggleGroup();
groupDifficulty.getToggles().addAll(
rdoEasy,
rdoMedium,
rdoHard
);
ToolBar toolBar = new ToolBar();
toolBar.setOrientation(Orientation.VERTICAL);
toolBar.getItems().addAll(
new Separator(),
rdoEasy,
rdoMedium,
rdoHard,
new Separator()
);
BorderPane borderPane = new BorderPane();
borderPane.setLeft(toolBar);
borderPane.setCenter(group);
borderPane.getCenter();
Scene scene = new Scene(borderPane, orgSceneX, orgSceneY,true,SceneAntialiasing.BALANCED);
I'm working on a school project in Java FX, I'd like to add an icon next to my primarystage title and center the title, just like the one in the picture. I have looked around in the javadoc for Stage but i can only find the setTitle() method. Here is a link to what i'd like to do :
You can set your stage to undecorated, set your scene root to a border pane and create your own custom top bar.
BorderPane root = new BorderPane();
root.setTop(new Pane(bb, label));
Scene scene = new Scene(root, 400, 400);
ButtonBar topBar = new ButtonBar(… insert buttons in here....);
// eg: Button b = new Button("X); b.setOnAction(e->{stage.close();});
root.setTop(topBar);
stage.initStyle(StageStyle.UNDECORATED);
I am trying to reuse my buttons after changing scene.
HBox area = new HBox();
area.getChildren().addAll(homeButton, loadAPubButton);
area.setSpacing(20);
area.setAlignment(Pos.CENTER);
// Home Scene
BorderPane homePane = new BorderPane();
homePane = setupHomeScene(homePane);
homePane.setTop(area);
home = new Scene(homePane,1280,960);
// Load a Pub Crawl Scene
BorderPane loadPane = new BorderPane();
loadPane = loadPubCrawl(loadPane);
loadPane.setTop(area);
loadPub = new Scene(loadPane,1280,960);
This code will use the same HBox on two different scenes however it doesn't seem to work. I am really stuck and any help would be much appreciated.
Just wondering if anyone know how to implement a vertical scrollbar that spans the entire scene in JavaFX? Been trying to figure it out for a few days now and I just can't seem to figure it out.
If you mean make it your root you can do it like this
Stage stage = new Stage();
ScrollPane pane = new ScrollPane();
TextField yourcontent = new TextField("this is an example");
pane.setContent(yourcontent);
pane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
Scene scene = new Scene(pane,300,300);
stage.setScene(scene);
stage.showAndWait();
and then put the rest of your content in it via setContent();
i've created fxml files in anchor pane
but everytime i click on the button i get the next fxml in a new frame
i want it to open in the same frame
public void baropen(ActionEvent event) {
// handle the event here
BorderPane bp = new BorderPane();
bp.setPadding(new Insets(10, 50, 50, 50));
Stage stage = new Stage();
Scene scene ;
// scene= new Scene(root);
scene = new Scene(bp);
stage.setScene(scene);
stage.show();
try {
new RecBar().start(stage);
} catch (Exception ex) {
Logger.getLogger(RecController.class.getName()).log(Level.SEVERE, null,ex);
}
}
Just create a single Stage and when you want to replace the content of the stage with a new fxml, load the new fxml into a new Scene and call stage.setScene.
It's a theater metaphor - imagine you are watching a play - it's Romeo and Juliet, the curtain raises and you see the first scene (a plaza in Verona with a fountain). Later on, the curtain lowers, little men run around and change stuff, the curtain rises and you see a new scene (the balcony to Juliet's bedroom). The scene has changed, but the stage has not - there is just one stage and multiple scenes.