JavaFx Children alignment between 2 HBoxes - javafx

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.

Related

HBox children on top without changing layout order

I have created a Hbox with several Buttons inside as children. I want to increase one button size and be on top of others. Not as appreciated in the figure... I Have tried with children_button.toFront() but it changes the layout order.
Any help would be appreciated.
i am not sure if this works in an Hbox as I think toFront puts the Child on the first position of the ChildList in the parent Node. But this would mean that HBox would layout it on the very left on first place. So maybe they are overriding the default behavior in this Case? Just my guess

Adding Shapes to GridPane results in wrong Position

I got the task to draw some points on a map. Wrote some code but currently every point I create via shapes will be added to the wrong position inside of my gridpane. Oh and I'm using JavaFX.
I added an imageView to the index 0,0 of my GridPane and every point is created through x and y position of the MouseEvent on the imageView.
After that I added the created point as a child of the GridPane and it's displayed at the center of the y-axis of the first grid.
Tried different things like anchorPanes and canvas but can't seem to get it working.
Code of my View:
http://pastebin.com/dCb7EN4d
Code of my Main:
http://pastebin.com/vp5tzxkG
I hope that's enough ^^'
pls help!
Greetings,
Ben
GridPane is a managed layout: it will position nodes that are added to it via the properties you set (using defaults if you don't set them). So when you add your circles to the grid pane, since you don't set any properties, it will place it in cell (0,0) and align it within that cell using default settings; i.e. it ignores the centerX and centerY properties.
What you should really do here is use a layout that does not manage the positioning of the nodes for you, such as a Pane (or possibly a Group). You can put the ImageView and the Circles in the pane, and then place the pane in the rest of your layout (in the scroll pane, I think).
The other option you have is to call setManaged(false) on the nodes you add to the GridPane in order to instruct the GridPane not to position them, though this feels like more of a workaround.

JavaFX - StackPane X, Y coordinates

I'm using StackPanel as container for my figures, panels, etc. What I discovered, that coordinates X,Y (0,0) are placed right in center of my panel.
Is it possible to move it to left top od Pane ?
Calculating all dimensions from center is much more difficult.
You can set the layout of Nodes added to the StackPane to a position within the Stackpane using the StackPane.setAlignment(node, position) method:
Label topLeftLabel = new Label("Top Left");
StackPane stack = new StackPane();
stack.getChildren().add(topLeftLabel);
StackPane.setAlignment(topLeftLabel, Pos.TOP_LEFT);
Even though this is possible, from your brief description of how you are trying to use the StackPane, it sounds like you would be better off using a regular Pane, or a Group or an AnchorPane for the kind of absolute positioning you appear to be wanting to achieve.
Possibly look into using a visual tool such as SceneBuilder as well. Even if you don't end up using the FXML it outputs, SceneBuilder should give you a much better idea of how JavaFX layout mechanisms work. SceneBuilder makes use of AnchorPane as its default layout pane used to provide absolute positioning for elements (which seems to be what you want to achieve).
The previous answer is of course the best in this situation, but it is also wise to know that you can move Nodes on the StackPane using Translation.
Ex.
Label topLeftLabel = new Label("Top Left");
StackPane stack = new StackPane();
stack.getChildren().add(topLeftLabel);
topLeftLabel.setTranslateX(stack.getWidth()/2);
topLeftLabel.setTranslateY(stack.getHeight()/2);
It would do the same thing (but may look a bit worse)

Flex 3: HBox children Scrolling with buttons

I have an HBox with width=500.
I actually want to add two arrows buttons that will scroll the contents of the HBox.
But when I turn HBox's scroll policy to off, I can't scroll it programmatically using horizontalScrollPosition.
What should i do now?
Thanks
I've hacked together this custom HBox that you could use. Simply set horizontalScrollPolicy to either "on" or "auto". I really haven't tested it all that much, works for a simple test I did...
public class CustomHBox extends HBox
{
override public function validateDisplayList():void
{
super.validateDisplayList();
if (horizontalScrollBar)
horizontalScrollBar.visible = false;
}
}
Scroll bars will not be displayed when scrollPolicy is turned off.
I think for what you want, you want to subclass ScrollBar make it look and feel the way you would like, then set it on your Container.horizontalScrollBar
I'm no Flex expert, but this is possible without too much trouble (using Flex SDK 3.2, anyway). You're right - when you turn off the horizontalScrollPolicy, the maxHorizontalScollPosition is set to 0, UNLESS you specify both a width value AND a maxWidth value. Then, maxHorizontalScrollPosition will again contain a useable value, and you'll be able to programmatically set the horizontalScrollPosition.

Flex: adding/removing component from HBox.rawChildren causes horizontalAlign to fail?

I have an HBox displaying a series of canvases. I am removing a child of a canvas and adding it to the rawChildren of the containing HBox, so I can position it, and make it appear to shift outside the bounds of the canvas.
Here is the code from the canvas:
private function onMouseOver(e:MouseEvent):void
{
(this.parent as HBox).rawChildren.addChild(dateLabel);
dateLabel.x = (this.parent as HBox).localToGlobal(new Point(this.x,0)).x - 18;
}
private function onMouseOut(e:MouseEvent):void
{
addChild(dateLabel);
dateLabel.x = 0;
}
It works, but if the containing HBox.horizontalAlign is set to "right", when I add the child back to the Canvas, the HBox stops displaying correctly and puts all the child canvases overlapping on the right. There is no issue if the HBox is aligned "left" tho.
Is this a bug? Is there a work around?
Thanks!!
Is this a bug? Is there a work around?
- John Isaacks
This isn't a bug as such, it's more that you are using a container in an unusual way.
When you use an HBox you are making a decision that all children are laid out in a linear, horizontal arrangement according to the rules of the HBox component.
Explicitly positioning a child is not what HBoxes are about - it's not in their job description.
I would recommend that you have an HBox inside a Canvas. You can add the dateLabel to the HBox when it should be laid out horizontally or move it to the Canvas when you need to set its position and make it look like it's outside the HBox.
When you use rawChildren, you simply bypass the layout mechanism.
You should use addChild or addChildAt directly on the component.

Resources