I am trying to add a ToolBar control, containing 2 Buttons and a TextField in my scene graph.
I want that the TextField control gets automatically resized and acquire all the space available into toolbar. So, I used the HBox layout control to add the buttons and textfield.
I did as followed:
ToolBar tb = new ToolBar();
HBox hbox = new HBox(8);
TextField tf = new TextField();
HBox.setHgrow(tf, Priority.ALWAYS);
hbox.getChildren().add(new Button("<-"));
hbox.getChildren().add(new Button("->"));
hbox.getChildren().add(tf);
tb.getItems().add(hbox);
But its not working. Where am I going wrong? Please help.
You can try next:
ToolBar tb = new ToolBar();
TextField tf = new TextField();
HBox hbox = new HBox(8);
hbox.prefWidthProperty().bind(tb.widthProperty().subtract(20));
HBox.setHgrow(tf, Priority.ALWAYS);
hbox.getChildren().add(new Button("<-"));
hbox.getChildren().add(new Button("->"));
hbox.getChildren().add(tf);
tb.getItems().add(hbox);
Related
So I have to make a Zodiac Sign GUI, and we are tasked with having the following:
a Label in the top left, and a TextField in the top right (both with padding)
an exit Button in the center of the GUI, along with a clear and find my sign on either side
and finally, a Label in the bottom center prompting the sign
I am utterly confused on how to have this come out, as I am a novice in JavaFX. I believe I would need a branch node along with the root node in order to get this kind of layout. I do not need assistance in instantiating the button, labels etc., mainly confused with how this layout can even work. The code I have now is the following:
public class ZodiacGUI extends Application {
public static void main(String args[]) {
Application.launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane mainPane = new BorderPane();
mainPane.setStyle("-fx-background-color: PINK");
setupControls(mainPane);
Scene scene = new Scene(mainPane);
setStage(primaryStage, scene);
}
public void setStage(Stage primaryStage, Scene scene) {
primaryStage.setWidth(500);
primaryStage.setHeight(200);
primaryStage.setTitle("What is my Zodiac Sign?");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setupControls(BorderPane mainPane) {
Label label = new Label("Enter you birthday formatted as -> mm/dd");
Button exitButton = new Button();
Button findSign = new Button();
Button clear = new Button();
TextField userInput = new TextField();
userInput.setPromptText("Enter birthday");
exitButton.setText("Exit.");
findSign.setText("Find my sign.");
clear.setText("Clear.");
exitButton.setOnAction(e -> System.exit(0));
mainPane.setLeft(label);
mainPane.setRight(userInput);
mainPane.setCenter(exitButton);
mainPane.setCenter(findSign);
mainPane.setCenter(clear);
BorderPane.setAlignment(label, Pos.TOP_LEFT);
BorderPane.setAlignment(userInput, Pos.TOP_RIGHT);
BorderPane.setAlignment(exitButton, Pos.CENTER);
BorderPane.setAlignment(findSign, Pos.CENTER_LEFT);
BorderPane.setAlignment(clear, Pos.CENTER_RIGHT);
}
}
This only outputs one of the buttons out of the three, as I assume it is because the necessary addition of another BorderPane? Here is a drawn out picture of what I would like to come out with:
Just to clarify, I do not need assistance with the handling of finding the zodiac sign, etc. Mainly need assistance on the layout, as it has stumped me for days. Thank you in advance for helping out a novice to JavaFX :).
You have three rows with diffrent number of children. You can use HBox if row have more than one child.
BorderPane mainPane = new BorderPane();
mainPane.setTop(new HBox(topLabel, topField));
mainPane.setCenter(new HBox(centerLabel, centerField, centerButtom));
mainPane.setBottom(bottomCenterButton);
If you need more than 3 rows (top, center, bottom section of BorderPane) you can use VBox, where every child is row like:
HBox row1 new HBox(child1, child2)
HBox row2 new HBox(child1, child2, child3)
HBox row3 new HBox(child1)
HBox row4 new HBox(child1, child2)
VBox pane = new VBox(row1, row2, row3, row4);
You might want to use a GridPane
GridPane grid = new GridPane();
grid.add(label,0,0);
grid.add(userInput,2,0);
grid.add(findSign,0,1);
grid.add(exitButton,1,1);
grid.add(clear,2,1);
or use a VBox with BorderPane to help with the layout/alignment
BorderPane mainPane, centerPane;
mainPane.setLeft(label);
mainPane.setRight(userInput);
centerPane.setLeft(findSign);
centerPane.setRight(clear);
centerPane.setCenter(exitButton);
BorderPane.setAlignment(label, Pos.LEFT);
BorderPane.setAlignment(userInput, Pos.RIGHT);
BorderPane.setAlignment(exitButton, Pos.CENTER);
BorderPane.setAlignment(findSign, Pos.LEFT);
BorderPane.setAlignment(clear, Pos.RIGHT);
VBox items = new VBox();
items.getChildren().addAll(mainPane, centerPane);
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 have managed to create a JFoenix tabpane on initialization. However, i do not know how to set certain actions to happen when my tabs are being selected. Below is my code!
tabPanel = new JFXTabPane();
tabPanel.setPrefSize(440, 50);
tab = new Tab();
tab.setText("My Events");
tab2 = new Tab();
tab2.setText("Categories");
tab3 = new Tab();
tab3.setText("Suggested Events");
tab3.setId("suggestedEventsTab");
tabPanel.getTabs().add(tab);
tabPanel.getTabs().add(tab2);
tabPanel.getTabs().add(tab3);
VBox tabBox = new VBox();
tabBox.setPrefSize(446, 100);
Pane tabPane = new Pane();
tabPane.setPrefSize(450, 100);
VBox.setMargin(tabPanel, new Insets(33 , 0 , 17 , 0));
tabBox.getChildren().add(tabPanel);
tabPane.getChildren().add(tabBox);
TopPane.getChildren().add(tabPane);
SingleSelectionModel<Tab> selectionModel = tabPanel.getSelectionModel();
//
Thank you in advance! Cheers
Add a ChangeListener to listen for ChangeEvents
JavaFX TabPane: How to listen to selection changes
http://www.java2s.com/Tutorial/Java/0240__Swing/ListeningforSelectedTabChanges.htm
I'm trying to make a chatBox with javafx, and I want the messages from the client to be aligned to right and the rest to left.
I'm using a Vbox, wrapped in a Scrollpane and in that Vbox, each message is wrapped in another Vbox.
But aligning the inner Vbox doesn't work.
Here is my code:
private VBox addMsg(String senderName, String text, String time) {
Label snderName = new Label(senderName + ":");
snderName.setId("senderName");
snderName.setMaxWidth(400);
snderName.setAlignment(Pos.BASELINE_LEFT);
Label msgText = new Label(text);
msgText.setId("msgText");
msgText.setWrapText(true);
msgText.setMaxWidth(400);
Label msgTime = new Label(time);
msgTime.setId("msgTime");
msgTime.setMaxWidth(400);
msgTime.setAlignment(Pos.BASELINE_RIGHT);
VBox msg = new VBox(snderName, msgText, msgTime);
msg.setBackground(new Background(new BackgroundFill(Color.AQUA, new CornerRadii(3d), Insets.EMPTY)));
msg.setPadding(new Insets(5));
msg.setMaxWidth(400);
msg.setEffect(new DropShadow(2, Color.DARKBLUE));
return msg;
}
public void buildChatBox() {
Button backToPublicChat = new Button("<");
backToPublicChat.setId("backToPublicChat");
backToPublicChat.setVisible(false);
Text chatWindowInfo = new Text("public chat room");
chatWindowInfo.setId("chatWindowInfo");
VBox chatHistory = new VBox();
chatHistory.setId("chatHistory");
chatHistory.setPrefWidth(CHAT_BOX_WIDTH);
chatHistory.setPrefHeight(GAME_HEIGHT - 50);//badsmell
TextField messageField = new TextField("type your message...");
messageField.setId("messageField");
messageField.setPrefHeight(30);
messageField.setPrefWidth(CHAT_BOX_WIDTH - 40);
Button sendButton = new Button("send");
sendButton.setId("sendButton");
sendButton.setPrefHeight(30);
sendButton.setOnMouseClicked(event -> {
VBox vBox = addMsg(
"Aran",
"hi, i'm aran",
"5:19");
vBox.setAlignment(Pos.TOP_LEFT);
chatHistory.getChildren().add(vBox);
VBox vBox2 = addMsg(
"Farnam",
"oh, I love you!",
"5:19");
vBox2.setAlignment(Pos.TOP_RIGHT);
chatHistory.getChildren().add(vBox2);
});
ScrollPane scrlPane = new ScrollPane(chatHistory);
scrlPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrlPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
scrlPane.setId("scrolPane");
chatBox = new VBox(new HBox(backToPublicChat, chatWindowInfo), scrlPane, new HBox(messageField, sendButton));
chatBox.setId("chatBox");
chatBox.getStylesheets().add("TowDef/GUI//chatBoxCSS.css");
}
Also I just need to set the HPos but i don't know how to, so I used the Pos.TOP_RIGHT.
does anyone know how to achieve this?
any advice on how to make the chatbox in a better way will be well appreciated:)
You can achieve that using HBox,
For Right Aligning.
Label label=new Label("guru ");
label.getStylesheets().add("sample/styles/send.css");
label.setId("receive");
HBox hBox=new HBox();
hBox.getChildren().add(label);
hBox.setAlignment(Pos.BASELINE_RIGHT);
vBox.getChildren().add(hBox);
vBox.setSpacing(10);
For Left Aligning
Label label=new Label(msg);
label.getStylesheets().add("sample/styles/send.css");
label.setId("send");
HBox hBox=new HBox();
hBox.getChildren().add(label);
hBox.setAlignment(Pos.BASELINE_LEFT);
vBox.getChildren().add(hBox);
vBox.setSpacing(10);
I want to add a title bar to my undecorated JavaFX Window and I display the default icons(close, minimize) to be displayed there.
My Stage contains A Border Pane with an HBox inside
BorderPane root = new BorderPane();
HBox title = new HBox();
Button close = new Button();
Button min = new Button();
title.getChildren().addAll(close, min);
root.setTop(title);
root.setBottom(grid);