package graphics.api.control; import javafx.event.EventHandler; import javafx.application.Application; import javafx.beans.value.*; import javafx.event.ActionEvent; import javafx.geometry.Bounds; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.text.*; import javafx.scene.control.*; import javafx.scene.input.*; import javafx.scene.paint.Color; import javafx.stage.Stage; public class HyperlinkFocusIssue extends Application { public static void main(String[] args) { HyperlinkFocusIssue.launch(args); } String focusOwnerStr;//moved this varialbe from run method TextBox textBox = new TextBox();//moved this varialbe from run method Hyperlink hyperlink = new Hyperlink();//moved this varialbe from run method float h;//moved this varialbe from run method float w;//moved this varialbe from run method Text focusText;//moved this varialbe from run method @Override public void start(Stage stage) throws Exception { h = 400; w = 400; focusOwnerStr = (textBox.isFocused()) ? (textBox.getId()) : ((hyperlink.isFocused()) ? (hyperlink.getId()) : ("UNKNOWN")); textBox.focusedProperty().addListener(new InvalidationListener() { public void invalidated(ObservableValue ov) { focusOwnerStr = (textBox.isFocused()) ? (textBox.getId()) : ((hyperlink.isFocused()) ? (hyperlink.getId()) : ("UNKNOWN")); focusText.setContent("Focus Owner: " + focusOwnerStr); } }); boolean visitedVal = false; textBox.setId("TextBox"); textBox.setText("www.google.com"); textBox.setColumns(10); textBox.setTranslateX(w / 2 - textBox.getLayoutBounds().getWidth() / 2); textBox.setTranslateY(h / 4 - textBox.getLayoutBounds().getHeight()); hyperlink.setId("Hyperlink"); hyperlink.setText(textBox.getRawText()); hyperlink.setFont(new Font(20.0F)); hyperlink.layoutBoundsProperty().addListener(new InvalidationListener() { public void invalidated(ObservableValue ov) { hyperlink.setTranslateX(w / 2 - hyperlink.getLayoutBounds().getWidth() / 2); hyperlink.setTranslateY(h / 2 - hyperlink.getLayoutBounds().getHeight()); } }); hyperlink.setTranslateX(w / 2 - hyperlink.getLayoutBounds().getWidth() / 2); hyperlink.setTranslateY(h / 2 - hyperlink.getLayoutBounds().getHeight()); hyperlink.setVisited(!visitedVal); hyperlink.setOnKeyPressed(new EventHandler() { @Override public void handle(KeyEvent ke) { if (ke.getCode().equals(KeyCode.LEFT)) { textBox.requestFocus(); } } }); focusText = new Text(10.0F, 20.0F, "Focus Owner: " + focusOwnerStr); focusText.setFont(new Font(10.0F)); focusText.setFill(Color.BLACK); Scene scene = new Scene(new Group(), w, h); ((Group) scene.getRoot()).getChildren().clear(); ((Group) scene.getRoot()).getChildren().addAll(textBox, hyperlink, focusText); stage.setScene(scene); stage.setVisible(true); } }