GridPane stay centered while window is resized - javafx

Currently my gridpane stays centred on the X axis while the window is resized, how can I get it to stay centred on the Y axis as well?
I have this in a tab in a TabPane:
GridPane grid = new GridPane();
grid.setPadding(new Insets(20, 0, 0, 0));
grid.setAlignment(Pos.CENTER);
grid.setHgap(20);
grid.setVgap(20);
this.setContent(grid);

The key here is VBox.setVgrow(tabPane, Priority.ALWAYS);
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication65 extends Application
{
#Override
public void start(Stage primaryStage)
{
VBox root = new VBox();
MenuBar mb = new MenuBar();
TabPane tabPane = new TabPane();
root.getChildren().addAll(mb, tabPane);
//StackPane stackPane = new StackPane();
CreateProfilePane cp = new CreateProfilePane();
tabPane.getTabs().add(cp);
VBox.setVgrow(tabPane, Priority.ALWAYS);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}

Related

JavaFX center text

I cannot figure out why, but the text on this is not centering on the screen no matter what I do, it is aligning on the left. please help.
public class FarmApp extends Application {
Scene scene;
HBox pane;
Stage gameStage;
/**
* {#inheritDoc}
*/
public void start(Stage stage) {
pane = new HBox();
Scene scene = new Scene(pane, 1500, 900);
createWelcome();
this.scene = scene;
gameStage = stage;
stage.setTitle("Chuggville");
stage.setResizable(false);
stage.setScene(scene);
stage.sizeToScene();
stage.show();
} // start
/**
* Creates the welcome screen for Farm game
*/
public void createWelcome() {
HBox hbox2 = new HBox();
Text chugville = new Text();
chugville.setText("Chugville");
chugville.setFont(Font.font("Verdana", 90));
BorderPane centerText2 = new BorderPane();
centerText2.setCenter(chugville);
hbox2.getChildren().addAll(centerText2);
pane.getChildren().addAll(centerText2);
}
}
You have so much noise in your code that it's hard to know what's causing the issue.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class App extends Application {
/**
* {#inheritDoc}
*/
#Override
public void start(Stage stage) {
BorderPane welcomeScreen = createWelcome();
Scene scene = new Scene(welcomeScreen, 1500, 900);
stage.setTitle("Chuggville");
stage.setResizable(false);
stage.setScene(scene);
stage.show();
} // start
/**
* Creates the welcome screen for Farm game
* #return
*/
public BorderPane createWelcome() {
Text chugville = new Text();
chugville.setText("Chugville");
chugville.setFont(Font.font("Verdana", 90));
BorderPane centerText2 = new BorderPane();
centerText2.setCenter(chugville);
return centerText2;
}
}

Align a Hbox to the bottom center inside a StakePanel

I'm learning Javafx layouts and I would like to obtain the following result:
HBox in the bottom center fo a Pane I have tried with the following code but I'm not able to do that
Can you please explain what I'm doing wrong? and How to achieve that?
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TestLayout extends Application {
#Override
public void start(Stage stage) {
initUI(stage);
}
private void initUI(Stage stage) {
Scene scene =null;
HBox hbox = new HBox();
hbox.setStyle("-fx-background-color: #FFFAAA;");
StackPane pane = new StackPane();
hbox.setStyle("-fx-background-color: #AAFAAA;");
hbox.prefWidthProperty().bind(pane.widthProperty().divide(4));
hbox.prefHeightProperty().bind(pane.heightProperty().divide(10));
pane.getChildren().add(hbox);
scene = new Scene(pane, 600, 600);
pane.prefWidthProperty().bind(scene.widthProperty());
pane.prefHeightProperty().bind(scene.heightProperty());
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class AppLauncherTest {
public static void main(String[] args) {
TestLayout.main(args);
}
}
I would Use a VBox or BorderPane as the root node. In this example, I use a VBox. I am assuming more nodes will go into this so I used a StackPane as the top node. This may need to be changed out or some other Pane may need to be added to this. That depends on what you are trying to do with the end product. For the bottom, I used an HBox and I set it's Margins.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Priority;
import javafx.geometry.Insets;
/**
* JavaFX App
*/
public class App extends Application {
#Override
public void start(Stage stage) {
StackPane subRootTop = new StackPane();
VBox.setVgrow(subRootTop, Priority.ALWAYS);
//subRootTop.setStyle("-fx-background-color: yellow;");
HBox subRootBotton = new HBox();
VBox.setVgrow(subRootBotton, Priority.ALWAYS);
subRootBotton.setStyle("-fx-background-color: green;");
subRootBotton.setMaxHeight(150);
VBox.setMargin(subRootBotton, new Insets(40, 40, 40, 40));
VBox root = new VBox(subRootTop, subRootBotton);
root.setStyle("-fx-background-color: red;");
Scene scene = new Scene(root, 600, 600);;
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
The binding of the StackPane is not size shouldn't be done. The scene automatically resizes its root to fill the whole area available. Assuming you want relative sizes, using StackPane is not really a good choice.
If you want the child to have a fixed distance to left right and bottom of the StackPane, you can do so specifying margins and alignment. Make sure the child does not grow to fit the parent though by setting the maxHeight to use the preferred height:
private void initUI(Stage stage) {
HBox hbox = new HBox();
hbox.setStyle("-fx-background-color: #FFFAAA;");
hbox.setMaxHeight(Region.USE_PREF_SIZE);
// could be calculated based on children instead of assigning
// an absolute value
hbox.setPrefHeight(30);
StackPane.setAlignment(hbox, Pos.BOTTOM_CENTER);
StackPane.setMargin(hbox, new Insets(20));
StackPane pane = new StackPane(hbox);
pane.setStyle("-fx-background-color: #AAFAAA;");
Scene scene = new Scene(pane, 600, 600);
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
Maybe you can use an AnchorPane as a base layout instead of the StacKPane. I made a small exmaple how it would look like. Of cause you could keep the StackPane as the base layout and just put the AnchorPane with the HBox onto it, but you should definitly use an AnchorPane for your plan.
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
private void initUI(Stage stage) {
// Create an anchor pane as base layout and set a color:
AnchorPane root = new AnchorPane();
root.setStyle("-fx-background-color: #FFFAAA;");
// Create a second container and set a minimum height and a color;
HBox hbox = new HBox();
hbox.setMinHeight(100d);
hbox.setStyle("-fx-background-color: #AAFAAA;");
// Give the child container a fixed location:
AnchorPane.setBottomAnchor(hbox, 50d);
AnchorPane.setLeftAnchor(hbox, 75d);
AnchorPane.setRightAnchor(hbox, 75d);
// Add the horizontal box to the base anchor pane:
root.getChildren().add(hbox);
stage.setScene(new Scene(root, 600, 600));
stage.show();
}
#Override
public void start(Stage stage) throws Exception {
initUI(stage);
}
public static void main(String[] args) {
launch(args);
}
}
Preview:

How to generate rectangle with user values in output?

I have seen examples of Rectangle in JavaFx. But Please can anybody Provide me the example where in output window/Scene, if user put desirable width and height, rectangle should be generated automatically.
here is my example
VBox vb = new VBox(20);
HBox h1 = new HBox(7);
HBox h2 = new HBox(7);
Label lebel1 = new Label("X:");
Label lebel2 = new Label("Y:");
TextField txt1 = new TextField();
TextField txt2 = new TextField();
//Converting textfield to integer only
ChangeListener<String> forceNumberListener = (observable, oldValue, newValue) -> {
if (!newValue.matches("\\d*"))
((StringProperty) observable).set(oldValue);
};
txt1.textProperty().addListener(forceNumberListener);
txt2.textProperty().addListener(forceNumberListener);
double width = Double.parseDouble(txt1.getText());
double height = Double.parseDouble(txt2.getText());
Rectangle rect1 = new Rectangle();
rect1.setHeight(height);
rect1.setWidth(width);
h1.getChildren().addAll(lebel1, txt1);
h2.getChildren().addAll(lebel2, txt2);
vb.getChildren().addAll(h1,h2,rect1);
If user put any integer value in "x" as width, "y" as height, Rectangle should be generated below fields. but this code is wrong and i don't know other methods. Please
Thank you so much
You should use the TextField's onkeyreleased event handler. In this app, if both TextFields have a number typed in, a rectangle will be generated. Both TextFields have an event handler that does the same thing if one of their text is changed. This does not catch for any non-double values.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication104 extends Application
{
#Override
public void start(Stage primaryStage)
{
BorderPane root = new BorderPane();
VBox vbox = new VBox();
vbox.setMinWidth(100);
TextField textfield1 = new TextField();
TextField textfield2 = new TextField();
textfield1.setPrefWidth(50);
textfield1.setPromptText("Enter height");
textfield1.setOnKeyReleased(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event)
{
if(textfield1.getText().length() > 0 && textfield2.getText().length() > 0)
{
Rectangle rectangle = new Rectangle();
rectangle.setHeight(Double.parseDouble(textfield1.getText()));
rectangle.setWidth(Double.parseDouble(textfield2.getText()));
rectangle.setFill(Color.BLUE);
root.setCenter(rectangle);
}
}
});
textfield2.setPrefWidth(100);
textfield2.setPromptText("Enter length");
textfield2.setOnKeyReleased(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event)
{
if(textfield1.getText().length() > 0 && textfield2.getText().length() > 0)
{
Rectangle rectangle = new Rectangle();
rectangle.setHeight(Double.parseDouble(textfield1.getText()));
rectangle.setWidth(Double.parseDouble(textfield2.getText()));
rectangle.setFill(Color.BLUE);
root.setCenter(rectangle);
}
}
});
vbox.getChildren().addAll(textfield1, textfield2);
root.setLeft(vbox);
Scene scene = new Scene(root, 500, 500);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}

JavaFX How to 'hide' TitledPane back after expanding

I have TitledPane, which i want to hide back (after expanding - un-expand it) after pressing a button. Is there any way to do it? I didn't find any way :( Thanks!
Just do
titledPane.setExpanded(false);
Complete example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TitledPaneExample extends Application {
#Override
public void start(Stage primaryStage) {
Label label = new Label("Some content");
Button button = new Button("OK");
VBox content = new VBox(10, label, button);
TitledPane titledPane = new TitledPane("Titled Pane", content);
button.setOnAction(e -> titledPane.setExpanded(false));
VBox root = new VBox(titledPane);
Scene scene = new Scene(root, 250, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

how to ensure visible a node into a TitlePane which is into a scrollpane in javafx

I have a scrollPane which has various TitledPane into this, each TitledPane has various textfield's as children. When i navigate throughout textfield's using Tab key, the scrollpane do not autoscroll to ensure visible the control gaining focus. This is a program demonstrating the problem.
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ScrollPaneEnsureVisible extends Application {
private static final Random random = new Random();
#Override
public void start(Stage primaryStage) {
VBox root = new VBox();
ScrollPane scrollPane = new ScrollPane();
Pane content = new Pane();
scrollPane.setContent(content);
for (int i = 0; i < 3; i++) {
TitledPane pane = new TitledPane();
AnchorPane paneContent = new AnchorPane();
for (int j = 0; j < 2; j++) {
TextField text = new TextField();
paneContent.getChildren().add(text);
text.setLayoutX(8);
text.setLayoutY(j*30);
}
pane.setCollapsible(false);
pane.setContent(paneContent);
pane.setLayoutY(i*100);
content.getChildren().add(pane);
}
root.getChildren().add(scrollPane);
scrollPane = new ScrollPane();
content = new Pane();
scrollPane.setContent(content);
for (int i = 0; i < 3; i++) {
TitledPane pane = new TitledPane();
AnchorPane paneContent = new AnchorPane();
pane.setContent(paneContent);
pane.setPrefWidth(200);
pane.setPrefHeight(80);
pane.setCollapsible(false);
pane.setLayoutY(i*100);
content.getChildren().add(pane);
}
root.getChildren().add(scrollPane);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) { launch(); }
}
For the second titledpane all is fine, but for first not. i need to sensure visible (autoscroll the scrollpane) that controls when they gain focus.

Resources