JavaFX scene builder questions - javafx-2

I'm working with JavaFX scene builder and have two questions.
Fisrt one:"How to add border to Pane in JavaFX scene builder?"
Second one: "How to split cells in HBox?"

I dont know why you would want to join HBox cells as you can set the resize behaviour for every child of the hbox.
There is a example in HBox's 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"));
Joining cells would be possible in a GridPane with the row- and/or columnSpan.
GridPane gridpane = new GridPane();
gridpane.add(new Button(), 0, 0, 2, 2); // column=0 row=0, spans over 2 columns and 2 rows
gridpane.add(new Label(), 3, 1); // column=3 row=1 (spans over 1 column and 1 row (default))
row-/columnSpan and the vertical horizontal Grow can be specified in the Properties bar of the Scene Builder, residing on the right side, by default.

You can set border by using setStyle() and use some styles like -fx-border
examples
P.S. styles are the same, as like css, but with -fx- prefix

Related

Three textviews with equal width in the same row (Table-layout)

I have a tablelayout in which each row consists of three textviews.
I don't know the number of rows so I can't set the height of the textviews from the XML layout and I need to do that programmatically.
The next code displays the textviews but not in proper height.. how to do that programmatically in the code?
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
row.setGravity(Gravity.CENTER);
tv1 =new TextView(this);
tv2 =new TextView(this);
tv3 =new TextView(this);
tv1.setText(a);
tv2.setText(b);
tv3.setText(c);
row.addView(tv1,0);
row.addView(tv2,1);
row.addView(tv3,2);
tablelayout.addView(row);
In case you want that the TableLayout height matchs to its parent
see this post:
Android: Stretching rows in TableLayout programmatically
and if you want to have columns with equal width have a look at this:
Set equal width of columns in table layout in Android [duplicate]

Setting LayoutParams but its not working android

Trying to create a linear layout programmatically and setting its width and height by layout params. But it seems layout params isn't working.
this is the code:
// CREATING A NEW LINEAR LAYOUT PROGRAMMATICALLY
LinearLayout linearLayout = new LinearLayout(getActivity());
ViewGroup.LayoutParams layoutParams = new
ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// CREATING CHILDDRENS (TEXT VIEWS)
TextView name = new TextView(getContext(), null, 0, R.style.item_layout_style);
name.setText("Pine");
TextView qty = new TextView(getContext(), null, 0, R.style.item_layout_style);
qty.setText("10");
TextView cost = new TextView(getContext(), null, 0, R.style.item_layout_style);
cost.setText("785");
TextView tCost = new TextView(getContext(), null, 0, R.style.item_layout_style);
tCost.setText("1000");
// SET TEXT VIEW TO LINEAR LAYOUT
linearLayout.addView(name);
linearLayout.addView(qty);
linearLayout.addView(cost);
linearLayout.addView(tCost);
// SET LINEAR LAYOUT TO PINE LAYOUT
LinearLayout daddy= (LinearLayout) view.findViewById(R.id.layout);
daddy.addView(linearLayout, 2);
// Return the view
return view;
I have root layout (daddy) which has many linear layouts (vertically oriented), but I need to create a linear layout programmatically and add that to "daddy". But the text views are sticked together, they aren't getting the entire space horizontally.
Do help me!
Everything is fine with the code. It is the style that I tried to give to text views, they weren't getting layout weight, width and height. How did I find this out? Well I set the linear layout's background-color to black to see if it's really not getting width and height set by LayoutParams. And I wasn't wrong! Width was set to match parent. So what I did was create a new layout param for text views and set it to them.

JAVAFX - Adding Nodes To TitledPane's Header

I want to add nodes into the TitledPane's header. The only way I found to do it is using setGraphic() method.
The problem of using that way is I cannot place them neatly. Here is what I get:
The Codes
TitledPane tpane = new TitledPane();
tpane.setContent(new Text("Content"));
tpane.setExpanded(false);
Button b = new Button("Delete");
BorderPane box = new BorderPane();
box.setLeft(new Text("1 "));
box.setCenter(new Text("Single Pane"));
box.setRight(b);
AnchorPane par = new AnchorPane(box);
tpane.setGraphic(par);
VBox vbox = new VBox();
vbox.getChildren().add(tpane);
The Goal
I want to put the Delete button to the right, that's why I am using Borderpane. So the BorderPane need to fill the entire of the TitledPane's header width minus the space for small triangle
How can it be done inside setGraphic() method? Or are there any better way to achieve it?
To fix your problem try setting the width of the BorderPane to the width of the scene minus the arrow like this.
TitledPane tpane = new TitledPane();
tpane.setContent(new Text("Content"));
tpane.setExpanded(false);
Button b = new Button("Delete");
BorderPane box = new BorderPane();
box.setLeft(new Text("1 "));
box.setCenter(new Text("Single Pane"));
box.setRight(b);
AnchorPane par = new AnchorPane(box);
tpane.setGraphic(par);
VBox vbox = new VBox();
vbox.getChildren().add(tpane);
box.setPrefWidth(scene.getWidth()-30);//You can adjust the number to fit your needs and change scene to whatever the name of your scene is
I hope this helped
Well, it seems that support for javafx users is not as massive as the mainstream framework.
After some tries by the suggestion from #Austin, I found the way to do what I want, and this is also give dynamic sizing.
box.prefWidthProperty().bind(scene.widthProperty().subtract(35));
TitledPane tpane = new TitledPane();
tpane.setContent(new Text("Content"));
tpane.setExpanded(false);
Button b = new Button("Delete");
BorderPane box = new BorderPane();
box.setLeft(new Text("1 "));
box.setCenter(new Text("Single Pane"));
box.setRight(b);
tpane.setGraphic(box);
box.prefWidthProperty().bind(scene.widthProperty().subtract(35));
VBox vbox = new VBox();
vbox.getChildren().add(tpane);

JavaFX TabPane not filling space

I've placed some content inside a TabPane's tab, but the TabPane's width seems to me being equal to the longest element inside the tab (the longest element is not visible on the picture).
The TabPane is inside an AnchorPane with zero valued anchors, that's why I would expect it to fill the whole space, not just the size of the longest element inside..
How could I make the TabPane fill all available space?
UPDATE:
I've forgot to mention, that my a TabView is loaded through the FXMLLoader.load() method and it's added as a child to the AnchorPane..
If you add the tabPane in the code, you could try like this :
AnchorPane anchorPaneContent = new AnchorPane();
TabPane node = new TabPane();
AnchorPane.setTopAnchor(node, 0.0);
AnchorPane.setLeftAnchor(node, 0.0);
AnchorPane.setRightAnchor(node, 0.0);
AnchorPane.setBottomAnchor(node, 0.0);
anchorPaneContent.getChildren().add(node);
If you add the tabPane in the SceneBuilder, you just need to make the tabpane fit it's parent by right clicking on the node.

How can I create resizing spacers in JavaFX?

First of all, I'm a long time Java/Swing developer. I recently installed JavaFX 2.2 to play around with.
I'm creating a fairly simple app, whose main window has a toolbar on top and content in the rest of the window. The obvious way to accomplish this is to use a BorderPane, and stick a ToolBar into the top section. So far, so good. However, I would like some of the controls in the toolbar to be at the left edge of the window, and some at the right edge. I can find no way to do this. I can put an invisible spacer object into the toolbar, but I only know how to give it a fixed width; it doesn't resize when the window is resized.
So I thought that instead of using a ToolBar object, I'll just use an HBox; it should be equivalent to a horizontally-oriented Swing Box object, right? And the Swing Box class has a createHorizontalGlue() method that inserts an auto-sizing spacer. Well, I can't find an equivalent in the JavaFX HBox class. Is there no simple way to do this?
I figured out how to do it using an HBox instead of a ToolBar to hold the controls; the key is the HBox.setHgrow() method, which allows you to set a spacer object to grow to fill the available space. I still don't know if it's possible to do this with an actual ToolBar instance.
/**
* Creates and populates the Node that serves as the window toolbar.
*
* #return a newly constructed and populated toolbar component
*/
private Node makeToolbar() {
// Auto-sizing spacer
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
// Horizontal box containing toolbar controls
HBox box = new HBox();
box.setPadding(new Insets(8));
box.setAlignment(Pos.CENTER);
box.getChildren().addAll(openButton, spacer, resizeSlider);
// Colored background panel with drop shadow
Pane bgRect = new Pane();
bgRect.setStyle("-fx-background-color: #e0e0e0;");
bgRect.setEffect(DropShadowBuilder.create().width(1).build());
// StackPane to hold box and rectangle
StackPane stack = new StackPane();
stack.getChildren().addAll(bgRect, box);
return stack;
}
i do it this way:
private Node makeFooter(Node left, Node right) {
ToolBar footer = new ToolBar();
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
spacer.setMinWidth(Region.USE_PREF_SIZE);
footer.getItems().addAll(left, spacer, right);
return footer;
}
hope i could help someone

Resources