I'm building a simple login registration form and I'm trying to use a text flow to switch the scene of the primary stage but for some reason, none of the event handlers I have attempted to use are working as intended
this is what i have tried so far
TextFlow flow = new TextFlow(new Hyperlink("Dont have an account?"));
flow.setOnMouseClicked(actionEvent -> primaryStage.setScene(scene2));
Most controls in JavaFX consume mouse events by default1. That means when you click on the Hyperlink
the event is consumed before it can bubble up to the TextFlow
, thus your event handler is never invoked.
You should probably be setting the onAction
property of the Hyperlink
anyway, as that's not only more correct semantically but also allows reacting to keyboard input in addition to mouse input. For example:
Hyperlink link = new Hyperlink("Don't have an account?");
link.setOnAction(e -> primaryStage.setScene(scene2));
TextFlow flow = new TextFlow(link);
// do something with 'flow'...
As an aside: Do you need to use TextFlow
here? If all you're doing is placing a Hyperlink
within the TextFlow
then you should probably just be using the Hyperlink
directly instead.
1. This is done by the javafx.scene.control.SkinBase
class. See its #consumeMouseEvents(boolean)
method.