onHiddenProperty event for PopOver Control - javafx

I have a PopOver ControlsFX and I want to create an event every time disappears. You searched information and I think it is done with the method onHiddenProperty but I cannot apply it correctly.

You can check if it has lost the focus which means that the popover disappears:
popOver.focusedProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue){
// oldValue is true -> current value is false-> no focus
// TODO your code here
}
});

Related

JavaFX width/height properties fire only once

I'm new to JavaFX and am trying to monitor the resize events on a window so that I can trigger a recalculation of the layout.
If I create a stage and set the scene like in the example below I only get each resize event to be fired once. No matter how many times I resize the window.
Stage stage = new Stage();
stage.setScene(someScene);
stage.setTitle("Some Title");
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
stage.widthProperty().addListener(observable -> {
System.out.println("Width changed");
});
stage.heightProperty().addListener(observable -> {
System.out.println("Height changed");
});
First note:
I'm trying to monitor the resize events on a window so that I can
trigger a recalculation of the layout.
This is almost certainly the wrong approach. Layout recalculations will be triggered automatically when the stage and scene change size. If you use standard layout panes, there is no need to register listeners. If you really need a custom layout (which is highly unlikely), you should subclass Pane and override layoutChildren() (and other appropriate methods) to hook into the same layout system.
However, in the interests of explaining what you're observing:
You're registering an InvalidationListener with each property, which gets notified when the property goes from a valid state to an invalid state.
The invalid state only becomes valid again if you actually request the value of the property (e.g. stage.getWidth()). Since you never do that, the property never becomes valid, and hence never goes from valid to invalid again.
Instead, register a ChangeListener with each property:
stage.widthProperty().addListener((observable, oldWidth, newWidth) -> {
System.out.println("Width changed");
});
stage.heightProperty().addListener((observable, oldHeight, newHeight) -> {
System.out.println("Height changed");
});
Alternatively, you can force validation by requesting the value (though I think the change listener above actually gets to the point of what you are trying to do):
stage.widthProperty().addListener(observable -> {
System.out.println("Width changed: "+stage.getWidth());
});
etc.

Show text on TextField after button is pressed (JavaFX)

I'm trying to build a calculator, and I have looked all over internet and the examples don't help me, so I have buttons created and everything, I'm trying to display on:
TextField Result = new TextField();
Result.setEditable(false);
Result.setAlignment(Pos.CENTER_RIGHT);
Result.setMinSize(210, 30);
Result.textProperty().bind(Bindings.format("%.0f" , value));
pane.getChildren().add(Result);
the number of the button pressed, how do I do that? Let's say the button was:
Button uno = new Button("1");
uno.setMinSize(40, 40);
pane.getChildren().add(uno);
How do I make the text field Result show number 1?
BIG Thanks!
The simplest way is to add a listener for each your button and change your TextField dynamically:
uno.pressedProperty().addListener((o, old, newValue) -> Result.setText("1"));
dos.pressedProperty().addListener((o, old, newValue) -> Result.setText("2"));
Note, when the user pressed and unpressed a button your listener will react twice with newValue = true/false. Do the additional checking if needed:
uno.pressedProperty().addListener((o, old, newValue) -> {
if (newValue) {
Result.setText("1")
}
});
UPDATE:
Don't forget to remove this line as wrong solution:
Result.textProperty().bind(Bindings.format("%.0f" , value));

How to know if the Spinner is increasing in JavaFX?

I would like to know if there is any method to know when the Spinner is increased.
The ideal would be to know the moment in which the Spinner increases
You can add a ChangeListener to the value property of the Spinner which will be notified of changes. Since the old and the new values are both passed to the changed method, it allows you to find out, if the value was increased or decreased:
Spinner<Integer> spinner = new Spinner<>(0, 100, 0);
spinner.valueProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue < newValue) {
System.out.println("value increased");
}
});
In the handler you could also retrieve the time, e.g. by using System.currentTimeMillis if this is necessary.

javafx setFocus after tabPaine change

Problem:
Have tabPane tabs OK.
In the first tab there is a text field. I am able to get focus on this field when starting the application.
After changing the tabs and coming back to the first tab I want focus to be on this textfield (barcodereader should be active in this field) without having to select the field with the mouse.
I am able to catch event from tabs with
tp.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>()
{ etc
(could not post with code)
and I am able to trigger en event for the first tab.
But field.requestFocus(); does not work. Probably because this method comes before rendering the textfield.
So here is my question:
How do you set focus on a control after clicking tabs in TabPane?
If you handle the mouse release event, it works: (The doFocus enables the requestFocus handling only when a tab selection changed before, otherwise it kicks in every time you click somewhere in the TabPane.)
final SimpleBooleanProperty doFocus = new SimpleBooleanProperty(false);
tabPane.setOnMouseReleased(new EventHandler<Event>() {
#Override
public void handle(Event event) {
if (!doFocus.get()) {
return;
}
doFocus.set(false);
switch (tabPane.selectionModelProperty().getValue().selectedIndexProperty().intValue()) {
case 0: tf1b.requestFocus(); break;
case 1: tf2a.requestFocus(); break;
default: break;
}
}
});
tabPane.selectionModelProperty().getValue().selectedIndexProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable,
Number oldValue, Number newValue) {
doFocus.set(true);
}
});
When the TabPane has focus, one can change tab selection with the cursor keys and there the TextFields also won't get the focus with selection based approach. This probably should be handled too, if you need it.
(Recently I had a similar problem. I noticed, that the TabPane switches tabs immediately when you press the mouse button. My guess would be, that the selection based approach requests focus on the TextField right after mouse down, but the continued mouse down steals the focus back to the TabPane. Or maybe even the single mouse down event which changes selection causes the focus to go back to TabPane. However, my assumptions regarding the reasons may not be correct, as I am a newbie to JavaFX.)
EDIT: That handling certainly is not optimal. For instance, if you change tabs with the keys, the doFocus will be enabled and then clicking anywhere in the TabPane will trigger the requestFocus call. I thought this should be mentioned.
Also, take a look at my solution for setting focus on TextArea, when user changes selected tab(using mouse or keyboard) https://stackoverflow.com/a/19046535/2791746

How can I cancel a DataGrid ToolTip before its shown?

I've got a DataGrid that shows a tooltip on each item.. but there are a few items where there shouldn't be a tooltip. So I thought I could prevent the being showed.
protected function toolTipStart(event:ToolTipEvent) : void
{
LOG.debug('Start ' + event);
event.stopImmediatePropagation();
}
But it does not work. Has anyone an idea?
regards
Cyrill
stopImmedatePropogation will just stop further event listeners from catching the event; it does not have a relation to whether or not the toolTip is created.
I'm pretty sure you set the toolTip property of the event to null; then the toolTip will not show up.

Resources