placement of dynamically added buttons in javafx - java

I have dynamically created a bunch of buttons. I gave a fixed width to each of them. I want it to be added until space is available to its parents and when it overlaps with some other nodes, it will automatically go to the next line in order to avoid collision. I want it to be responsive at different screen sizes.
for(Data i:datas){
Button btn=new Button(i.getName());
anchorPane.getChildren().addAll(btn);
}
I want to arrange the buttons in a new line only when the buttons in the first line have covered its total width. How can I accomplish it? Any help is appreciated.

James_D provided a great resource—there's a built in layout for anything you need and FlowPane has what you want here.
Using your example, just swap out your AnchorPane for FlowPane:
FlowPane flowPane = new FlowPane();
for(Data i:datas){
Button btn=new Button(i.getName());
flowPane.getChildren().addAll(btn);
}
To make it responsive (i.e. expand and contract with the parent container/stage), bind the FlowPane's width and height to those of its parent:
flowPane.prefWidthProperty().bind(stage.widthProperty());
flowPane.prefHeightProperty().bind(stage.heightProperty());

Related

Free scroll pane (JavaFX)

What pane can I use to add anchorpanes(setting y and z).
Initially it is of a certain size, and if it is too many anchorpanes to show, i`d like to add a scroller with which I can scroll down the pane to see more elements.
P.S.
sry for my bad english
This Question is very simple to answer. Just use a Scroll Pane and a Element to group your Anchor-Panes, for example :
ScrollPane with VBox as Grouping
Or you can also use Frameworks for this like :
http://www.jfoenix.com/
http://fxexperience.com/controlsfx/
When I remember correctly, they designed a Pane which is automatically placing nodes like a Gallery. These nodes get moved when necessary.

Java FX - How to create a Label with no contents to reserve spot in Layout Manager?

Is there a way to create a Label with some type of invisible contents, to preserve it's "space" in a layout such as a HBox or VBox so as to prevent Layout Manager engaging in some type of resizing, so that if I have to set the contents of a certain Label to empty, the Layout Manager will not resize the container?
Thanks!
All layouts will lay out an invisible node as if it is visible. You can use an invisible Label as a “strut” by making it invisible and placing it, along with your visible node, in a StackPane:
Label valueLabel = new Label("This may become empty");
Label strut = new Label(valueLabel.getText());
strut.setVisible(false);
StackPane labelPane = new StackPane(strut, valueLabel);
hBox.getChildren().add(labelPane);
Another option is to simply make your value Label invisible instead of making its text empty, but I realize there are circumstances where that may not be possible, such as if the Label’s text property is bound.
As with all Regions you can set the minWidth property:
label.setMinWidth(100);
Which will result in the label not being resized below size 100 regardless of it's text.

How to create dual HBox using javafx

I am trying to make controls for a media player. I have a slider and volume control which i wish to sit above another row of controls such as play mute etc at the bottom of the window.
I thought a good way to achieve this was creating a HBox that sits above another HBox, both aligned to the bottom in a javafx application.
I tried to use insets and margins to offset one from the other but i was not able to get them both to show.
Another attempt was to add the two HBox nodes to a VBox to get them vertically placed, but that was not successful either (i don't think it can be done this way).
Does anyone know how to implement this? or is there a better way to achieve the desired effect?
A GridPane might be what your looking for.
It acts similar to HBox and VBox, such as you can get creative and add other grids and boxes inside the main grid.

How to keep JavaFX MenuBar in the top part of the window

I'm trying to make an window, where I have StackPane as root and I want to add MenuBar to this window. However MenuBar is in the center of the screen and I want to keep it in the top part of the window as in normal Windows applications.
root = new StackPane();
root.getChildren().add(new MenuBar());
this will show window like this
http://i61.tinypic.com/2pzblmo.jpg
Thanks you for your advice!
I would say StackPane is not suitable for making a GUI including a menubar.
StackPane will just put the controls you add to it one on top of the other.
In java docs you can find:
"StackPane
The StackPane layout pane places all of the nodes within a single stack with each new node added on top of the previous node. This layout model provides an easy way to overlay text on a shape or image or to overlap common shapes to create a complex shape. Figure 1-6 shows a help icon that is created by stacking a question mark on top of a rectangle with a gradient background.
"

How to shift the position of tabbed panes

I know that when I create an instance of tabbed panes I set the position as such:
jtp = new JTabbedPane(SwingConstants.LEFT)
My question is that is it possible to set the position to the left as above, but to shift the starting position a little bit downwards? I do not want the first tab appearing at the very top of the left hand side. I want to leave some empty space. Is that possible? Thanks.
You can pack your component into javax.swing.Box add shift it:
jtp.add("Component shifted vertically", shiftVertical(your_component, 5)); // 5 px or more
public static Box shiftVertical(JComponent component, int size){
Box vBox = Box.createVerticalBox();
vBox.add(Box.createVerticalStrut(size));
vBox.add(component);
return vBox;
}
I managed to find a work around for this problem. I did this by creating a "dummy" icon which was basically a square with the same colour as the background colour of the tabbed panes. I then set this icon as the icon for a "dummy" tabbed pane and disabled the tab:
jtp.addTab("", dummyIcon, null);
jtp.setEnabledAt(0, false);
By resizing the icon I could make the first tabbed pane take up as much space as i wanted since the panes don't have to be the same size. This way The first tab was not clickable and it had a colour similar to the background colour of the panel, so it appeared as if the space was empty.

Categories

Resources