Skip to content

Latest commit

 

History

History
46 lines (35 loc) · 1.21 KB

File metadata and controls

46 lines (35 loc) · 1.21 KB

Overview

  1. Users cannot select text on a JavaFx Label
  2. Solution below

Example SelectableLabel class

/**
 * Behaves like a javafx.scene.control.Label, but user can select text.
 *
 * <p>User experience is more like text in a browser
 */
public class SelectableLabel extends TextField {

    public SelectableLabel() {
        super();
        actLikeLabel();
    }

    public SelectableLabel(String text) {
        super(text);
        actLikeLabel();
    }

    private void actLikeLabel() {
        // -- No mutation
        setEditable(false);

        // -- Tab traversal skips this
        //    (same as javafx.scene.control.Label)
        setFocusTraversable(false);

        // -- Look like a javafx.scene.control.Label
        getStyleClass().add("label");

        // -- Stop looking like a javafx.scene.control.TextField :-)
        getStyleClass().remove("text-input");
    }
}

Other Resources

  1. https://openjfx.io/javadoc/19/javafx.graphics/javafx/scene/doc-files/cssref.html
  2. https://github.com/openjdk/jfx/blob/jfx20/modules/javafx.controls/src/main/resources/com/sun/javafx/scene/control/skin/caspian/caspian.css