0

I was wondering how to make the caret visible and/or flash whilst using a TextField or TextArea. The ones I have created in my GUI work as the letters appear when typed but there is no visible caret.

I've looked through the TextField documentation but none are about making it visible or not. I expected to find something along the lines "setCaretVisible(Boolean);"

Do I have to make it visible via CSS? If so, any suggestions would be most welcome!

Please see code that I've quickly put together to illustrate the problem:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Test extends Application {
    public static void main(String[] arguments) { launch(arguments); }

    @Override public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        scene.setRoot(new BuildLayout(stage));

        stage.setTitle("Application Name");
        stage.setScene(scene);
        stage.initStyle(StageStyle.UNDECORATED);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setFullScreen(true);
        stage.setFullScreenExitHint("");
        stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
        stage.show();
    }
}

final class BuildLayout extends BorderPane {
    protected BuildLayout(Stage stage) {
        TabPane tabpane = new TabPane();

        Tab tab = new Tab();
        tab.setGraphic(new Text("Video Browser"));
        tab.setClosable(false);
        tab.setContent(new Input(1));

        tabpane.getTabs().addAll(tab);
        setTop(new Toolbar(stage));
        setCenter(tabpane);
    }
}

final class Input extends VBox {
    Input(int id) {
        HBox hbox = new HBox();
        TextField videoTitle = new TextField("video_title");
        TextArea description = new TextArea( "video_description");
        Button share = new Button("share");
        Button unshare = new Button("unshare");

        setPadding(new Insets(5,10,10,5));
        setAlignment(Pos.TOP_CENTER);

        hbox.getChildren().addAll(videoTitle, new Text("   Creator: " + "   Date: " + "   Views: " + "   "));

        if(true) {
            videoTitle.setEditable(true);
            description.setEditable(true);
            if(true) {
                hbox.getChildren().add(share);
            } else { hbox.getChildren().add(unshare); }
        }
        getChildren().addAll(hbox, description);
    }
}

final class Toolbar extends HBox {
    protected Toolbar(Stage stage) {
        Button close = new Button("close");
        close.setOnAction((ActionEvent e) -> { stage.close(); });

        setAlignment(Pos.CENTER_LEFT);
        getChildren().addAll(close);
    }
}

Many thanks,

macourtney7
  • 521
  • 2
  • 10
  • 24
  • 1
    Typically the carat is visible if and only if the text input control has keyboard focus. Are you seeing something different to that, or do you mean that you want the carat visible even when the control does not have focus? – James_D Feb 12 '15 at 15:43
  • On click when typing is possible in that specific field, not all the time. When I type into my fields there are just letters appearing. – macourtney7 Feb 12 '15 at 15:51
  • I don't understand that last comment. Probably you need to create an [MCVE](http://stackoverflow.com/help/mcve) demonstrating the issue, and edit your question to include it. – James_D Feb 12 '15 at 15:54
  • @James_D I apologise for that. I've put some code together to better demonstrate the behaviour. – macourtney7 Feb 12 '15 at 17:40
  • I guess you didn't read the "minimal" part of the MCVE link... ;). This looks like a bug. It seems that the `focused` properties of the text input controls (and other controls?) is not getting set when the application is in full-screen mode. (Notice the blue focus border is also missing from the controls.) Let me see if I can figure out a workaround... – James_D Feb 12 '15 at 17:54
  • Nothing I try seems to work completely. I can get the focus borders to appear by registering a listener with the scene's `focusOwnerProperty` and updating the `focused` `PseudoClass` accordingly, but that doesn't make the carat appear (probably it relies on the node's `focusedProperty`). The problem does seem fixed in the early-access release of JDK 8 update 40; my best suggestion is that you update to that if you can. BTW, are you using a Mac, by any chance? – James_D Feb 12 '15 at 18:09
  • I am indeed using a Mac. It's not an essential issue but it is an annoying one. I'll take a look at your suggestion. Thanks for your efforts. – macourtney7 Feb 12 '15 at 18:17
  • It may well be platform-specific, but I don't have a windows box to test on right now. – James_D Feb 12 '15 at 18:32
  • I've just asked a peer to run on Windows and the issue is **not** there. I've also changed setFullScreen(true) to setMaximized(true) and the caret has returned on Mac. The issue with this is that it doesn't maximise properly so I can't use this call. – macourtney7 Feb 12 '15 at 19:19

1 Answers1

0

This problem only occurs on Mac and not Windows (untested Linux). The fix is to downgrade to a JavaFX 2.2 compliant maximised window as the setMaximized() from JavaFX 8 also isn't Mac compatible.

Modifying the start method with some code found here:

@Override public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    scene.setRoot(new BuildLayout(stage));

    Screen screen = Screen.getPrimary();
    Rectangle2D bounds = screen.getVisualBounds();

    stage.setTitle("Application Name");
    stage.setScene(scene);
    stage.setX(bounds.getMinX());
    stage.setY(bounds.getMinY());
    stage.setWidth(bounds.getWidth());
    stage.setHeight(bounds.getHeight());          
    stage.show();
}

Produces a fullscreen application with visible and flashing caret.

Community
  • 1
  • 1
macourtney7
  • 521
  • 2
  • 10
  • 24
  • If you think it is a bug or regression, you should [file a bug report](https://javafx-jira.kenai.com). – jewelsea Feb 13 '15 at 18:42