Why is myLabel content not showing in my gridpane, it is strange, everything else is showing..
I have tried everything I can. any help please
here are snippets of my code and attached photo of the result
#Override public void start(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root, 1180, 650);
stage.setScene(scene);
stage.setTitle("Prayer Time Display");
scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());
GridPane Mainpane = new GridPane();
scene.setRoot(Mainpane);
Mainpane.setGridLinesVisible(true);
Mainpane.setId("Mainpane");
GridPane prayertime_pane = new GridPane();
prayertime_pane.setId("prayertime_pane");
prayertime_pane.setPadding(new Insets(20, 20, 20, 20));
prayertime_pane.setAlignment(Pos.BASELINE_CENTER);
prayertime_pane.setVgap(7);
prayertime_pane.setHgap(35);
GridPane moon_pane = new GridPane();
moon_pane.setGridLinesVisible(true);
moon_pane.setPadding(new Insets(10, 10, 10, 10));
moon_pane.setAlignment(Pos.BASELINE_CENTER);
moon_pane.setVgap(10);
moon_pane.setHgap(10);
myLabel.setText("hello");
moon_pane.setConstraints(myLabel, 0, 0);
moon_pane.getChildren().add(myLabel);
ImageView Moon_img = new ImageView(new Image(getClass().getResourceAsStream("/Images/Full_Moon.png")));
Moon_img.setFitWidth(100);
Moon_img.setFitHeight(100);
moon_pane.add(Moon_img, 1, 0);
Text next_moon_text = new Text("13/02");
next_moon_text.setId("prayer-text-english");
moon_pane.setHalignment(next_moon_text,HPos.LEFT);
moon_pane.setValignment(next_moon_text,VPos.CENTER);
moon_pane.setConstraints(next_moon_text, 2, 0);
moon_pane.getChildren().add(next_moon_text);
Mainpane.add(moon_pane, 8, 1,3,2);
Your label does appear on the scene but it has been shrinked to the extent that only the text overflow characters are rendered - "..."
To ensure that the label is always rendered with a fixed width that will fit all the text you can force the minumum and maximum widths to be the same as the preferred width:
myLabel.setMinWidth(Region.USE_PREF_SIZE);
myLabel.setMaxWidth(Region.USE_PREF_SIZE);
If you want to retain the dynamic sizing you can always use Scenic View (http://fxexperience.com/scenic-view/) to analyse your scene graph and determine which nodes or containers take up all the space or have size limits
Related
I want to make a border pane with two HBoxes on top and bottom and a GridPane in the center... I wrote what I needed, attached the labels but I can't run the code
Exception in Application start method
java.lang.reflect.InvocationTargetException is what I get as an error... the code is below, any help is welcome :) thanks
public class labelBorder extends Application {
#Override
public void start(Stage primaryStage) {
BorderPane bp = new BorderPane();
bp.setPrefSize(400, 400);
HBox hb1 = new HBox();
Label lb1 = new Label("");
lb1.setPrefWidth(200);
lb1.setBorder(new Border(new BorderStroke(Color.AQUAMARINE,BorderStrokeStyle.SOLID,null,new BorderWidths(5))));
Label lb2 = new Label("");
lb2.setPrefWidth(200);
lb2.setBorder(new Border(new BorderStroke(Color.BLUEVIOLET,BorderStrokeStyle.SOLID,null,new BorderWidths(5))));
HBox hb2 = new HBox();
URI foto = Paths.get("D:\\Barca.jpg").toUri();
Label lb3 = new Label();
lb3.setGraphic(new ImageView(foto.toString()));
lb3.autosize();
GridPane gp = new GridPane();
Label lb4 = new Label("");
Label lb5 = new Label("");
Label lb6 = new Label("");
Label lb7 = new Label("");
gp.add(lb4, 0, 0);
gp.add(lb5, 0, 1);
gp.add(lb6, 1, 0);
gp.add(lb7, 1, 1);
gp.getChildren().addAll(lb4,lb5,lb6,lb7);
hb1.getChildren().addAll(lb1,lb2);
hb2.getChildren().addAll(lb3);
bp.setTop(hb1);
bp.setCenter(gp);
bp.setBottom(hb2);
bp.getChildren().addAll(hb1,hb2,gp);
Scene scene = new Scene(bp);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
You should NOT add the duplicate children controls to your parent layout(pane). Your code must be throwing,
java.lang.IllegalArgumentException: Children: duplicate children added
To overcome your issue remove these lines,
gp.getChildren().addAll(lb4,lb5,lb6,lb7);
And,
bp.getChildren().addAll(hb1,hb2,gp);
As those controls have already been added to your corresponding layout.
Note: Adding children with pane.getChildren().addAll(...) on BorderPane is irrelevant and will be ignored while rendering the added controls.
I want a ScrollPane in my application window which doesn't fill the whole scene, but only takes up part of it, because I want to place a button beneath it.
Like in this sketch: https://i.imgur.com/eUA7Af7.png
I tried using a GridPane, but I can only put Nodes as children of the GridPane, which doesn't work because the ScrollPane is not a Node.
public void start(Stage stage) throws Exception {
var root = new GridPane();
var scene = new Scene(root, 400, 400);
var scrollPane = new ScrollPane();
root.add(scrollPane, 0, 0); // Not possible
var button = new Button();
root.add(button, 0, 1);
stage.setScene(scene);
stage.show();
}
Slaw's comment:
javafx.scene.control.ScrollPane definitely is a javafx.scene.Node. The class hierarchy is: ScrollPane → Control → Region → Parent → Node
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...
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.
I have to rewrite existing app, with SWT.
I'm adding FXCanvas class with JavaFX components on it.
I noticed that somehow my panes (usually, HBox'es) do not change size when I maximize window, for example.
My code is:
final Display display = parent.getDisplay();
shell = new Shell(display);
Group group = new Group();
scene = new Scene(group, Color.rgb(shell.getBackground().getRed(), shell.getBackground().getGreen(),
shell.getBackground().getBlue()));
fxCanvas = new FXCanvas(shell, SWT.NONE) {
#Override
public Point computeSize(int wHint, int hHint, boolean changed) {
getScene().getWindow().sizeToScene();
int width = (int) getScene().getWidth();
int height = (int) getScene().getHeight();
return new Point(width, height);
}
};
fxCanvas.setScene(scene);
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(20); // Gap between nodes
group.getChildren().add(hbox);
Please advise.