Hi guys I m completely new with GUIs... so far all of my programs where text based. I ve just started and im using JAVAFX.
I wanted to recreate a telephone keypad. It should have 4 rows of 3 buttons each and each button should have its related number along with its letters. And it does. But the size of the buttons is not the same.
I mean… since i'm using a tilePane the buttons occupy the same amount of pixels (or at least I guess it is like that), but the actual an visible size of the buttons is different because every button takes only the size that it needs in order to display its contents. I stored the buttons in an array. Is there a way to make them all of the same size of the biggest one?
public class PhoneKeyboard extends Application
{
Button buttons [];
#Override
public void start(Stage stage) throws Exception
{
// Create the set of necessary buttons.
buttons = new Button[12];
fillButtons(buttons);
//Create the grid for the buttons.
TilePane pane = new TilePane();
pane.setPadding(new Insets(10,10,10,10));
pane.setPrefColumns(3);
pane.setMaxWidth(Region.USE_PREF_SIZE);// Use the pref size as max size to make sure there will be the expected size
pane.setVgap(5.0);// to set ome spacing between each tile of the pane.
pane.setHgap(5.0);
//Put the buttons on the pane (layout);
pane.getChildren().addAll(buttons);
// JavaFX must have a Scene (window content) inside a Stage (window)
Scene scene = new Scene(new StackPane(pane), 300,400);// In order to use the pfer size of the pane, the tile Pane doesn"t have to be the root in the scene. Therefore we created a scene with a stack pane containing our tile pane as the root.
stage.setTitle("Keyboard");
stage.setScene(scene);
// Show the Stage (window)
stage.show();
}
A Button has a maximum width and maximum height which are just big enough to hold the Button’s content. Most layouts, including TilePane, won’t make child nodes larger than their maximum size.
If you want each button to be capable of being made larger, set its maximum width and height, after calling fillButtons:
for (Button button : buttons) {
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}
Related
I want to make a form with three sections, two with fields and one with buttons.
public class Form extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
StackPane root = new StackPane();
GridPane fp = new GridPane();
fp.setAlignment(Pos.TOP_CENTER);
fp.setHgap(6);
fp.setVgap(6);
fp.add(new Label("Name: "), 0, 0);
TextField name = new TextField();
name.setPrefWidth(450);
fp.add(name, 1, 0);
GridPane sp = new GridPane();
sp.setAlignment(Pos.CENTER);
sp.setHgap(6);
sp.setVgap(6);
sp.add(new Label("Another Name: "), 1, 0);
TextField anothername = new TextField();
anothername.setPrefWidth(120);
sp.add(anothername, 2, 0);
HBox hbox = new HBox();
hbox.setAlignment(Pos.BOTTOM_CENTER);
Button btn1 = new Button("Button 1");
hbox.getChildren().add(btn1);
Scene scene = new Scene(root, 500, 500);
root.getChildren().addAll(fp, sp, hbox);
stage.setScene(scene);
stage.show();
}
}
formatting and some text might be off but that is my general solution. I made a root stack pane to hold all the parts of my form. I then made two grid panes to hold text fields and an hbox to hold my buttons along the bottom.
example of how it looks
My problem is that only the name field can be clicked. If I try to click another name field it wont work. I can press tab to cycle through the fields and button but I want to be able to click on each field individually. Is there a better way to create one scene with multiple panes or hboxes? I am also open to only having one grid pane, but I thought having two would be easier for formatting since I want to separate different fields. Thank you!
The issue you're facing is caused by your using a StackPane as the root element of your scene.
A StackPane, as the name suggests, stacks its children one on top of the other. Any children placed on top will be the ones receiving events (such as clicking on the anothername field).
You have added 3 nodes as children of your StackPane:
GridPane #1 fp
GridPane #2 sp
HBox hbox
Since the HBox was added last, it is the only node that can receive click events.
Using your example, I've added borders to each of the 3 items above to illustrate how JavaFX is laying them out:
As you can see, each child of the StackPane get resized to fill the entire area (I used different widths for the borders so you can see them all).
You can try this yourself by adding the following code before you show your stage:
fp.setStyle("-fx-border-color: green; -fx-border-width: 15px");
sp.setStyle("-fx-border-color: blue; -fx-border-width: 10px");
hbox.setStyle("-fx-border-color: red; -fx-border-width: 5px");
To solve this, you will need to rethink your layout entirely; a StackPane is certainly not the correct layout pane to use in your case.
I highly recommend working through the examples in Oracle's Working With Layouts in JavaFX tutorial to get a better grasp on how to best layout your scene.
Is it possible to have multiple Buttons set to Bottom (left, center, right)?
This is what I tried:
private Pane createPane() {
BorderPane rootPane = new BorderPane();
rootPane.setTop(createMenueBar());
rootPane.setCenter(createTableView(model.getIssues()));
rootPane.setBottom(createDeleteIssueButton());
rootPane.setBottom(createCloseIssueButton());
rootPane.setBottom(createCreateNewIssueButton());
BorderPane.setAlignment(deleteIssueButton, Pos.BOTTOM_LEFT);
BorderPane.setAlignment(closeIssueButton, Pos.BOTTOM_CENTER);
BorderPane.setAlignment(createIssueButton, Pos.BOTTOM_RIGHT);
return rootPane;
}
Result:
As you can see it only shows the last added Button. What is the best way to get this done with JavaFX/BorderPane? I'm very new to this so let me know if you need any more info!
Nested layouts
Gather the multiple buttons into a layout manager. Place that layout manager object in the bottom position of your BorderPane.
For example, you might choose FlowPane as your layout manager.
FlowPane buttons = new FlowPane() ;
buttons.getChildren().addAll( deleteIssueButton , closeIssueButton , createIssueButton ) ;
The BorderPane places only a single widget in the bottom slot. You want your container of buttons to be that widget.
BorderPane rootPane = new BorderPane();
rootPane.setBottom( buttons ) ;
Your use of Pos.BOTTOM_LEFT and such determines where the widget is placed within the bottom slot. The BOTTOM in BOTTOM_LEFT means bottom slot of the given space within a slot, not the bottom of the BorderPane. Two different bottoms involved here.
BorderPane.setAlignment( buttons , Pos.CENTER ) ;
I want to have an HBox container with 3 buttons that are even in width, but when the parent's width can't be divided into whole number parts one of the nodes is less. If my HBox is 245px and I have 3 buttons 1 of them is 81px and the others are 82px.
The problem is that on top of the HBox I have a loading circle indicator and the circle is in the center of the HBox and when the middle button is not centered the loading circle also looks uncentered on top of the HBox.
HBox root = new HBox();
root.setFillHeight(true);
for (int i = 0; i < 3; i++) {
AnchorPane pane = new AnchorPane();
HBox.setHgrow(pane, Priority.ALWAYS);
pane.setMaxHeight(Double.MAX_VALUE);
root.getChildren().add(pane);
}
Scene scene = new Scene(root, 245, 50, Color.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.show();
root.getChildren().forEach(node -> {
AnchorPane pane = ((AnchorPane)node);
System.out.println(pane.getWidth());
});
The idea is that its a login scene and after submiting the username and password, the server is loading and there is a circle ProgressIndicator on top of the hbox and the circle is centered on top of it.
The circle looks uncentered on top of the middle button. So how can I do this with a layout container without explicity setting the width of the buttons. Do layouts always devide children on whole numbers?
So after some property testing I found out that snapToPixel=false is the way to go.
From oracle docs about snapToPixel https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Region.html
Defines whether this region adjusts position, spacing, and size values of its children to pixel boundaries. This defaults to true, which is generally the expected behavior in order to have crisp user interfaces. A value of false will allow for fractional alignment, which may lead to "fuzzy" looking borders.
I tried different px and in my case it works fine with the edges.
Also without touching the snapToPixel I found that GridPane with 33.3% columns width palces the 81px button in the middle, where with hbox it's the last child, so GridPane also worked for me.
I need to make a chat-simulating JavaFX application for my assignment (no web functionalities, just two text fields for input and two text areas for output). I need to have a “Send” button next to my text field. I cannot make the text field to fill up the width of my window without “squishing” the button at startup, similarly to how swing’s boxLayout does that?
I bound the field’s width to the parent pane’s width, subtracting the button’s width and the pane’sspacing and it works after I start resizing the window, but when the application first starts up, the button’s text is not fully visible.
public void start(Stage stage_main) throws Exception {
//Pane creation and nesting:
HBox pane_main = new HBox();
Scene scene_main = new Scene(pane_main, 480, 360);
BorderPane pane_left_parent = new BorderPane();
BorderPane pane_right_parent = new BorderPane();
HBox pane_left_bottom = new HBox();
HBox pane_right_bottom = new HBox();
pane_main.getChildren().addAll(pane_left_parent, pane_right_parent); //Focusing only on the left pane for now for testing.
pane_left_parent.setBottom(pane_left_bottom);
//Contents creation and nesting:
TextArea textA_left = new TextArea("Testing...");
Button button_left = new Button("Send");
TextField textF_left = new TextField("Test input...");
textF_left.prefWidthProperty().bind(pane_left_bottom.widthProperty().subtract(button_left.widthProperty()).subtract(pane_left_bottom.spacingProperty()));
//Placing contents in panes:
pane_left_parent.setCenter(textA_left);
pane_left_bottom.setSpacing(3);
pane_left_bottom.getChildren().addAll(textF_left, button_left);
//Finishing up:
stage_main.setScene(scene_main);
stage_main.show();
}
Is there any way to have the button have it’s “best” size already at startup without manually setting any widths in pixels, just like in swing?
Don't bind the prefWidth to the parent. If you want a child of an HBox to grow horizontally you can set an hgrow constraint on it:
HBox.setHgrow(theChild, Priority.ALWAYS);
Then let the HBox handle sizing and positioning the child node. And as you noted, in order to stop the Button from shrinking as the HBox changes size you need to set its minWidth. However, you should use:
button.setMinWidth(Region.USE_PREF_SIZE);
If you use getWidth() you might accidentally call it before the Button actually has a non-zero width. Plus, using USE_PREF_SIZE means the minWidth will stay up-to-date with the prefWidth (if you change it for whatever reason).
Some links:
HBox#.setHgrow(Node,Priority)
Priority
Region#USE_PREF_SIZE
I have a project that has a 2 text areas and few buttons. The root pane is a AnchorPane. when resizing the window to smaller window, all the elements start overlap. What methods can fix this? (IGNORE THE NAME OF MY anchorpane, i got lazy)
AnchorPane borderpane = new AnchorPane ();
TextArea user_list = new TextArea();
user_list.setPrefSize(150, 400);
TextArea messages = new TextArea();
messages.setPrefSize(350, 400);
TextField typebox = new TextField();
typebox.setPrefSize(425, 100);
// put a shape over a text, over a shape
StackPane send_container = new StackPane();
Rectangle send_box = new Rectangle(75, 25);
Label send_text = new Label("Send");
send_container.getChildren().add(send_box);
send_container.getChildren().add(send_text);
AnchorPane.setLeftAnchor(messages, 25.0);
AnchorPane.setTopAnchor(messages, 10.0);
AnchorPane.setRightAnchor(user_list, 25.0);
AnchorPane.setTopAnchor(user_list, 10.0);
AnchorPane.setBottomAnchor(typebox, 25.0);
AnchorPane.setLeftAnchor(typebox, 25.0);
AnchorPane.setBottomAnchor(send_container, 25.0);
AnchorPane.setRightAnchor(send_container, 25.0);
borderpane.getChildren().addAll(messages, user_list, typebox,send_container );
Scene scene = new Scene(borderpane, 600, 600);
primaryStage.setMaxHeight(600);
primaryStage.setMaxWidth(600);
primaryStage.setScene(scene);
primaryStage.setTitle("Welcome");
scene.getStylesheets().add(LoginWindow.class.getResource("Login.css").toExternalForm());
primaryStage.show();
You are hard-coding the locations and sizes of your controls. This means the controls cannot respond to changes in the size of their parent nodes.
Usually, you should not specify any heights or widths. Controls all have default preferred sizes, and all layouts respect those. Layouts also decide how child nodes will be resized in response to the user's resizing of a window.
Often, the layout of a window needs to be broken down into sub-layouts. In your case, you want one section that always resizes to fill the window (the user list and message section), with another section at the bottom (the typebox and Send button). A BorderPane is the ideal choice, since its center node always fills it. So the center of this main BorderPane would contain the user list and message area, while the bottom of this BorderPane would contain the typebox and the Send button.
You probably want the user to be able to horizontally resize both the user list and the messages, so I'd put them in a SplitPane, and make that SpiltPane the center of the main BorderPane.
You probably want the typebox and Send button to be in a separate child BorderPane, with the typebox as the center node, since you want the typebox to stretch and shrink, horizontally, when the user resizes the window.
So, to summarize:
user list and message area in a SplitPane
typebox and Send button in a BorderPane
parent BorderPane with user list/message section in the center, typebox/Send section on the bottom
The code for this is actually pretty short:
ListView user_list = new ListView();
TextArea messages = new TextArea();
messages.setPrefRowCount(12);
messages.setPrefColumnCount(30);
TextField typebox = new TextField();
typebox.setPrefColumnCount(30);
Button send_text = new Button("Send");
send_text.disableProperty().bind(
typebox.lengthProperty().lessThan(1));
SplitPane top = new SplitPane(user_list, messages);
top.setDividerPosition(0, 1/3.0);
BorderPane bottom = new BorderPane();
bottom.setCenter(typebox);
bottom.setRight(send_text);
BorderPane.setMargin(typebox, new Insets(0, 12, 0, 0));
BorderPane main = new BorderPane();
main.setCenter(top);
main.setBottom(bottom);
BorderPane.setMargin(bottom, new Insets(12));
Scene scene = new Scene(main);
primaryStage.setScene(scene);
primaryStage.setTitle("Welcome");
scene.getStylesheets().add(LoginWindow.class.getResource("Login.css").toExternalForm());
primaryStage.show();
Notice that there are no hard-coded dimensions or coordinates (except the margins defined by the Insets objects). Every control has a preferred size based on its properties, such as a TextField's preferred column count.
The workings of the various layouts are well documented. I suggest reading about them in the javafx.scene.layout package.
(I'm guessing the user list should be a ListView, not a TextArea, since typical chat programs allow selection of one or more users. And I suspect your black Rectangle and send_text Label were intended to represent a disabled Button.)
Use a pane other than a Anchor Pane. It's for absolute positioning. Try a stack pane or simple VBox.