I have an Hbox which contains Vboxes and each Vbox itself contains other Elements which can be added or removed and some control Buttons. Now I want to add or remove those flexible Elements in my Model and I need to know in which Vbox this is happening.
My attempt was to get the Children of the Hbox and check where the Vbox, where something happend/changed, is located in this List. And work with this Index afterwards.
public int getId (Action event){
Button button= event.getSource();
Vbox vbox= button.getParent();
Hbox hbox= vbox.getParent();
hbox.getChildrenUnmodifiable();
....//TODO
}
The Problem is that if I Print hbox.getChildrenUnmodifiable(); it shows me that:
[VBox#1402dd44, Separator#4eaff333[styleClass=separator], Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT]
and after I add an other Vbox it just adds the
Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT]
part again and again and doesen't show me any more information of the Vboxes which have been added.
I load the Vboxes from an other .fxml file with hbox.getChildren().add(FXMLLoader.load(getClass().getResource("/VBoxElement.fxml")));
Now I wonder if that looks like an proper way to get the Indexnumber of my Vbox where I am working on? And if anyone has any advice how to get a working Index of my Hbox?
This is my first JavaFX project and if I miss understood something feel free to correct me.
As James_D sayd:
ObservableList is just a subinterface of java.util.List, so you can
just call all the usual methods: indexOf(...), add(..), remove(...),
etc – James_D May 14 at 15:41
Thx, worked fine. As I expected it as first but I messed up my fxml structure and my added fxml did not contain a VBox as root element it contained a Grid. The toString() from the Grid produces the Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT in the output
Related
I'm creating an app in which I use nested FXML nodes. They are connected to parent nodes in the parent controllers using constructs like
#FXML
private AnchorPane nestedNodeConnector;
, to which I attach the child like this:
AnchorPane child = createNestedAnchorPaneWithTableViewInside();
nestedNodeConnector.getChildren().setAll((AnchorPane)child);
The code is a bit simplified, but I hopefully explained myself enough. Now my problem is that I am trying to load (as the child node) an as-large-as-possible AnchorPane with an anchored TableView inside. However I can't get this to work as the TableView dimension never sticks to the parent node (in order to grow with the window size).
I was able to get it working without nesting the child, but I really have to load the TableView in a separate FXML file. Any suggestions? I think that my approach creates an AnchorPane "child" within an AnchorPane "nestedNodeConnector", which messes up the anchor properties.
All I had to do was anchor the child AnchorPane (to the parent AnchorPane):
AnchorPane child = createNestedAnchorPaneWithTableViewInside();
AnchorPane.setTopAnchor(child, 10.0);
AnchorPane.setBottomAnchor(child, 10.0);
AnchorPane.setLeftAnchor(child, 10.0);
AnchorPane.setRightAnchor(child, 10.0);
nestedNodeConnector.getChildren().setAll((AnchorPane)child);
Trying to align children(labels) of 2 different HBoxes, which was created by the same method:
public HBox addHBox() {
HBox hbox = new HBox(10);
hbox.setLayoutX(10);
hbox.setPrefWidth(Pane.getWidth());
hbox.setPrefHeight(Pane.getHeight());
return hbox;
}
The LayoutY is different, and changed after the creation of each HBox.
Thus, I want to accompish that added child[i] of the HBox1, would be right below child[i] of the HBox2.
Note: the size of the children should not matter!
(this is what bothers me)
I tried
hbox2.setMinWidth(hbox1.getMinWidth());
and
hbox2.setPrefWidth(hbox1.getPrefWidth());
and
hbox2.setMaxWidth(hbox1.getMaxWidth());
Nothing seems to help. Any ideas?
P.S. I know that i can use gridPane, but it's not in this case
Your HBox only classifies your Nodes one side of the other horizontally, and in your case I suppose that you use a Text control to display your String, I don't say that it is a bad idea but I think that the width of your Node(Text) depends on the length of the string which gives an unpredictable and disorderly display, so why not use a Label, the advantage with this control is that you can define the Width of the background what Will give your String a limit and allow an ordered display !
Okay, after additional research AND help of the community:
1.Definately should use gridPane.
2.Or change the layoutX of each label in HBox2 to fit layoutX of label from HBox1.
I'm having difficultly understanding JavaFX CSS type selectors. I've had a good look at the official documentation
If I want to customize the spacing of layout elements such as HBox and VBox using the -fx-spacing rule do I have to add a custom CSS class to them all manually before I can reference them??
hBox.getStyleClass().add("hbox")
vBox.getStyleClass().add("vbox")
There doesn't seem to be a type based selection like this:
hbox {
-fx-spacing: 5
}
HBox and VBox are documented as "Style class: empty by default" I'm used to HTML where you can select by element type, e.g. div, button etc. I've got a lot of layouts and I don't want to have to go through them all.
Have you tried with capital letters?
HBox {
-fx-spacing:10px;
}
I can add elements to an AnchorPane that is inside a ScrollPane. However they come in temporarily hidden. The second I click on the AnchorPane, the elements show up. I tried to do thing like element.toFront(), but with no success. Any ideas how to get elements to show up right away?
Sample code would like:
AnchorPane host= new AnchorPae();
ScrollPane sp = new ScrollPane(host);
AnchorPane node = new AnchorPane();
host.getChildren().add(node);
I tried things like requestFocus() on each of these nodes, but without success. I also tried to mimic a mouse click event but it did not work. everything shows once I physically click on the AnchorPane.
Is there a way to make the elements of a VBox smoothly move to their new positions when a new element is inserted or removed?
I actually need only to make them move smoothly when I remove an element. Thank you for your answers!
Not without extending VBox and adding your own code to do so by overriding the addChild and removeChild functions.