Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copiable Label/TextField/LabeledText in JavaFX

I just want to create copiable label in JavaFX. I have tried to create TextField that have no background, have no focus border and default background color, but I have no success. I have found a lot of questions how to remove focus background from control but all of that looks like "hacks".

Is there are any standard solution to implement copyable text?

like image 783
zapletnev Avatar asked Mar 20 '14 13:03

zapletnev


People also ask

What is the difference between Label and TextField in Java?

In general, if you want to show the purpose of an input control by placing one or more words next to it, and/or you want to allow direct keyboard navigation to an input control, you use a Label. If you want to display text content not associated with input, you use Text.

How do you initialize a Label in JavaFX?

Syntax to Initialize JavaFX label is: Label lbl = new Label(); Here, the constructor can be of parameterized and non-parameterized, which depends on the requirement.

How do I change the text in a JavaFX Label?

Set Label Font You can change the font used by a JavaFX Label by calling its setFont() method. This is useful if you need to change the size of the text, or want to use a different text style.


2 Answers

You can create a TextField without the border and background color with css:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CopyableLabel extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField copyable = new TextField("Copy this");
        copyable.setEditable(false);
        copyable.getStyleClass().add("copyable-label");

        TextField tf2 = new TextField();
        VBox root = new VBox();
        root.getChildren().addAll(copyable, tf2);
        Scene scene = new Scene(root, 250, 150);
        scene.getStylesheets().add(getClass().getResource("copyable-text.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

and

copyable-text.css:

.copyable-label, .copyable-label:focused {
    -fx-background-color: transparent ;
    -fx-background-insets: 0px ;
}
like image 56
James_D Avatar answered Sep 20 '22 22:09

James_D


This is the solution I used, where there is a small button besides the label to be able to copy the text:

import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.Glyph;

import java.util.Locale;

public class CopiableLabel extends Label
{
    public CopiableLabel()
    {
        addCopyButton();
    }

    public CopiableLabel(String text)
    {
        super(text);
        addCopyButton();
    }

    public CopiableLabel(String text, Node graphic)
    {
        super(text, graphic);
    }

    private void addCopyButton()
    {
        Button button = new Button();
        button.visibleProperty().bind(textProperty().isEmpty().not());
        button.managedProperty().bind(textProperty().isEmpty().not());
        button.setFocusTraversable(false);
        button.setPadding(new Insets(0.0, 4.0, 0.0, 4.0));
        button.setOnAction(actionEvent -> AppUtils.copyToClipboard(getText()));
        Glyph clipboardIcon = AppUtils.createFontAwesomeIcon(FontAwesome.Glyph.CLIPBOARD);
        clipboardIcon.setFontSize(8.0);
        button.setGraphic(clipboardIcon);
        setGraphic(button);
        setContentDisplay(ContentDisplay.RIGHT);
    }
}
like image 22
Eng.Fouad Avatar answered Sep 19 '22 22:09

Eng.Fouad