Disable button depending on another node children amount in JavaFX - javafx

I need to disable button depending on some element children amount.
I have tried something like this, which is not right:
HBox userDataHBox = new HBox(new Label("1"), new Label("2"), new Label("3"));
Button btn = new Button();
btn.disableProperty().bind(
Bindings.notEqual(userDataHBox.getChildren().size(), 3)
);

userDataHBox.getChildren().size()
just yields the current size of the list. Nothing to observe there. You could use Bindings.size to get a IntegerBinding for the size that can be used:
btn.disableProperty().bind(
Bindings.size(userDataHBox.getChildren()).isNotEqualTo(3));

Here you go:
btn.disableProperty().bind(
Bindings.createBooleanBinding(()-> userDataHBox.getChildren().size() != 3, userDataHBox.getChildren())
);

Related

Placing buttons on opposite sides of an adjustable window in JavaFX

I have two buttons that I'd like to use in my display screen: a reset button, and a settings button.
The problem is, I want them on opposite sides of the window (reset in the top right corner and settings in the top left) while being the same height. When I expand the window, however, the buttons stay in the same place, but I want them to move with the window walls.
I first tried using setAlignment of my reset button to Pos.*TOP_RIGHT* and the settings button to top left, but it comes out with the settings button being on top of the reset button.
resetButton = new Button("Reset Gameboard");this.setTop(resetButton);
resetBox = new HBox(resetButton);
resetBox.setAlignment(Pos.TOP_RIGHT);
backButton = new Button("Back to Settings");
this.setTop(backButton);
backBox = new HBox(backButton);
backBox.setAlignment(Pos.TOP_LEFT);
resetbackBox = new HBox(backButton, resetButton);
this.setTop(resetbackBox);`
I currently am using (below) which during the initial window appears to be working and correctly spaced, but as soon as you expand the window the reset button doesn't follow it, although the settings button does.
`resetButton = new Button("Reset Gameboard");
this.setTop(resetButton);
resetBox = new HBox(resetButton);
resetBox.setAlignment(Pos.TOP_RIGHT);
backButton = new Button("Back to Settings");
this.setTop(backButton);
backBox = new HBox(backButton);
backBox.setAlignment(Pos.TOP_LEFT);
resetbackBox = new HBox(backButton, resetButton);
resetbackBox.setSpacing(380);
this.setTop(resetbackBox);`
The problem start from this line:
resetbackBox.setSpacing(380);
You have setted a fixed spacing value. To fix that you can easily add a component to HBox called Region, this component allow to set a grow area between two buttons:
Button resetButton = new Button("Reset Gameboard");
Button backButton = new Button("Back to Settings");
Region region = new Region();
HBox.setHgrow(region, Priority.ALWAYS);
HBox topHBox = new HBox(backButton, region, resetButton);
this.setTop(resetbackBox);

SetPadding of one single elemet in a layout whith many childrens

There is a way to set a Padding of one single element in a VBox having other childrens?
this.layout_p = new VBox();
this.txta_p = new TextArea();
this.m_p = new Button("m");
this.o_p = new Button("o");
this.c_p = new Button("c");
this.oa_p = new Button("oa");
this.np_p = new Button("np'");
layout_p.getChildren().addAll(txta_p, m_p,
o_p, c_p, oa_p, np_p);
layout_p.setAlignment(Pos.CENTER_RIGHT);
I would like to have the button: np_p with a top padding of 50 from others buttons.
So having a separation between oa_p and np_p.
(Without using another VBox for that button and so, setPadding(...))
Setting a padding for a single node does not make sense. Padding is the room between the bounds of a node and it's content, see also the CSS Box Model.
You're actually trying to add a margin here, i.e. some space around the node. This can be done using VBox.setMargin.
VBox.setMargin(np_p, new Insets(50, 0, 0, 0));

set spacing among elements within button javafx

I want to specify spacing between two elements within JavaFX Buttons, Here is the Code :
ImageView fiv = new ImageView(new Image("/modified/map.png"));
fiv.setFitHeight(20);
fiv.setPreserveRatio(true);
Button cr = new Button( "Crop", fiv);
Here I want to Specify Spacing Between "Crop" and fiv, How can i do this ?
Use graphicTextGap property of the button.

viewStack.addChild adding multiple child and only the last child is visible!

var viewStack:ViewStack = new ViewStack();
viewStack.percentWidth = 100;
viewStack.percentHeight = 100;
viewStack.addChild(canVas1);
viewStack.addChild(canVas2);
viewStack.addChild(canVas3);
lb = new LinkBar();
lb.dataProvider=viewStack;
Its only displaying canVas3 contents not canVas1 and canVas2
thanks
I think that's what it's supposed to do. It just puts components one on top of the other like a deck of cards, with the topmost (last added) being visible. Check this out.

Flex: removedEffect causes error when changing parent

ok I have a subclass of TitleWindow with this method:
public function launchInNewWindow(e:Event):void
{
this.parent.removeChild(this);
ownWindow = new Window();
ownWindow.systemChrome = 'none';
ownWindow.type = NativeWindowType.LIGHTWEIGHT;
ownWindow.transparent = true;
ownWindow.setStyle('showFlexChrome', false);
ownWindow.width = this.width > 750 ? 750 : this.width;
ownWindow.height = this.height > 550 ? 550 : this.height;
edit.enabled = false;
ownWindow.addChild(this);
ownWindow.width += 5; //add to show dropshadow
ownWindow.height += 10; //add to show dropshadow
ownWindow.open();
_inOwnWindow = true;
ownWindow.nativeWindow.x = Application.application.nativeWindow.x + this.x + 5; //keep in same spot add 5 for systemChrom border
ownWindow.nativeWindow.y = Application.application.nativeWindow.y + this.y + 30;//keep in same spot add 30 for systemChrom title
}
What this does is make the title window its own Window (NativeWindow) by creating a new Window object and adding itself to the new Window's displayList.
It works really well, however if I have a removedEffect set on the instance of this class it produces an error when trying to add itself to the Window's displayList.
I tried adding:
this.setStyle('removedEffect',null);
and
this.setStyle('removedEffect',new TitleWindow().getStyle('removedEffect'));
to the method as an attempt to remove any removedEffect set on itself before hand, but with no luck.
but it works fine if there is no removedEffect on the component. There has got to be a way to fix this.
Any ideas?
Thanks!!
For the removedEffect to work the window needs to be on the original parent, but you're immediately trying to add it to the new parent and it can't belong to two parents at once. There are a few options I can think of.
Get a bitmap of the window to be removed, show that in the same place, run the effect you want on this bitmap copy, and then you can run re-parent the original without interference from the effect.
Separate the code that removes and re-parents into two steps. Remove the window first. Then when the removedEffect is done, add it to the new window's display list.

Resources