I have a couple of buttons and want to collect them in a HBox and set them in a vertical row. The goal is something similar to this formation: http://vandelaydesign.com/images/navi/vertical.gif
Button addButton = new Button("Add Expense");
addButton.setOnAction(new AddExpenseGUI(rootStage, data));
Button editButton = new Button("Edit");
addButton.setOnAction(new EditButtonListener());
I tried multiple things. This was one idea of me, but I just get a horizontal formation:
HBox a = new HBox();
a.getChildren().addALL(addButton, editButton);
grid.add(a,0,0);
Any ideas?
Use a VBox and skin your buttons. See these:
Skinning JavaFX Applications with CSS
Styling FX Buttons with CSS
The H in HBox stands for Horizontal so the Pane you are looking for is VBox (for Vertical)
See:
http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm#CHDGHCDG
Related
I am new to javafx.
I am trying to build a messenger where the chatting panel show messages.
I want to align the messages like my own messages are on left and other messages are on right.
To display the message i am using TextFlow where I add Text. But the alignment isnt working.
TextFLow tf=new TextFlow();
Text t1= new Text("Hi");
Text t2= new Text("Hello");
t1.setTextAlignment(TextAlignment.RIGHT);
t2.setTextAlignment(TextAlignment.LEFT);
tf.getChildren().addAll(t1,t2);
But the alignment not working. Both the text are on left.
What should I do?
You can set the TextAlignement from the TextFlow but, i don't think we could have two alignment in the same container, however, you can use a tricky way to skirt this problem by using Labels & VBox:
private VBox Chat(){
VBox chat = new VBox();
chat.setPrefSize(400, 400);
chat.setStyle("-fx-background-color:#333333;");
Label txt1 = new Label("Text1");
txt1.setTextFill(Color.WHITE);
txt1.setPrefWidth(400);
txt1.setAlignment(Pos.CENTER_LEFT);
Label txt2 = new Label("Text2");
txt2.setTextFill(Color.WHITE);
txt2.setPrefWidth(400);
txt2.setAlignment(Pos.CENTER_RIGHT);
chat.getChildren().addAll(txt1,txt2);
return chat;
}
Why Label ? This node work in the same way as the Text node but the advantage is that wet can resize the background.
Why VBox ? is optional, even if i think TextFlow is better suited to textual nodes. It is also for the positioning, VBox is more suited for classified items from top to bottom chatting panel. Good luck !
I have a problem adding a gridpane to an AnchorPane in Scene Builder! The GridPane is completely out of shape as soon as I put it into the window. The actual window is at the position where it is supposed to be but the grid with the rows and columns is displaced. A screenshot shows what I mean:
Gridpane
Try updating scene builder to 2.0 or higher and re-add the GridPane or try changeing the layout settings from USE_COMPUTED_SIZE to constants. Or if it comes to it add the GridPane in java by doing:
#FXML
AnchorPane root;//This will be at the top of the page bellow where you define your class
GridPane gridPane = new GridPane();
root.getChildren().add(GridPane);//switch root out for the name of you anchor pane
I hope this helps.
I'm wondering if is it possible to wrap in my main Group Panel in other kind of panels ?. Actually i can do it with code but i want to deal with it only from Scene Builder to arrange components more easily. For example you can see a simple code section of how i manage to wrap my main Group Panel in Boder Pane.
// My main panel which initialized automatically to Group Panel
rootPane = FXMLLoader.load(getClass().getResource("Risk3.fxml"));
FlowPane flowPane = new FlowPane();
//Text Box 1
TextArea countryInfoText = new TextArea();
countryInfoText.setPrefWidth(100.0);
countryInfoText.maxWidth(100.0);
flowPane.getChildren().add(countryInfoText);
flowPane.setPrefWidth(countryInfoText.getPrefWidth());
BorderPane borderPane = new BorderPane();
borderPane.setCenter(rootPane);
borderPane.setLeft(flowPane);
scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
but Scene Builder doesnt let me wrap this root (or Group Panel in other words) in other Panels. You can see the snapshot below as an example.
Hope i had been clear to you and i will appreciate a lot for every response. So thanks anyway.
ok guys i made it. The trick is you have to wrap the panel with changing the source code of fxml file. So i included the line
<BorderPane id="BorderPaneDocument" fx:id="mainPanel" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication1.RiskControllerClass">
<center>
<Group>
.
.
</Group>
</center>
</BorderPane>
which Group was the main panel i wanted to wrap in BorderPane. So i created a Border Panel in fxml file and put the group panel on center of Border Pane. It works perfectly with this way and also i can modify every part from Scene Builder.
The layout of my spinner while close is adopting the layout of the layout configured to each row of the spinner.
The layout for each row is defined inside getCustomView method because I am using a custom adapter to the spinner, here it is define:
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false); //here is the layout for each row -> R.layout.row
//there are more things here but doesnt matter for the question..
}
On that layout (row.xml) I have margins top and bottom, because I want to give soem space between the choices.. But then the button itself of the spinner, I mean, the layout of the spinner while closed also has the margins.. and it looks larger than what I want.
It is possible to define different layouts?
I tryed to make the spinner layout at xml using less height, but then I can see the option clearly.. because it has margins..
Did I make myself clear?
Thanks alot in advance.. If someone didnt understand I can explain better.
Thanks ;)
Finnaly found a solution to this problem :)
Didn't know that getDropDownView was for the dropdown layout and the getView was for the single layout, at closed spinner...
Solution here: Android Spinner with different layouts for "drop down state" and "closed state"?
I want to have a menu for my program. And I like the standard Menu look and all, but I want to place a "logout-button" on the far right side of the menu-bar.. is it possible to place it there WITHOUT having to fill up the whole menu-bar with entries?
Sincerely
Yes you can. Use the HBox#setHgrow();. This javadoc page also has an example how to use it in "Optional Layout Constraints" section. Following is taken from javadoc.
For example, if an hbox needs the TextField to be allocated all extra space:
HBox hbox = new HBox();
TextField field = new TextField();
HBox.setHgrow(field, Priority.ALWAYS);
hbox.getChildren().addAll(new Label("Search:"), field, new Button("Go"));
Briefly speaking, set Priority.ALWAYS for the button (or any control) just before the "logout-button" in a HBox. More advanced example is here: Using Built-In Layout Panes : Example 1-4