Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Javafx Label selectable

Is there anyway to make a Label text selectable in JavaFx8? I know, there are other simple workaround like using a TextField. But my label needs multiline text with wrapping facility which TextField does not provide. If I use TextArea, the problem is I can't shrink the TextArea based on the text's size like a Label. So I can't use either of them.

Also my use of label text is like below:

<VBox>
    <Label wrapText="true"
           VBox.vgrow="ALWAYS"
           maxHeight="Infinity" minHeight="-Infinity"
           text="Some Random Subject Line With Very Large Text To Test The Wrap Text, Lorem Ipsum Dolor"/>                       
</VBox>

Depending on the VBox width, Label's height resizes to fit the text fully. I can't emulate this kind of behaviour using either TextArea or TextField. But I need to be able to select the text from Label. Any ideas?

like image 553
Anindya Chatterjee Avatar asked May 25 '17 06:05

Anindya Chatterjee


People also ask

How do you declare a Label in JavaFX?

Creating a Label You create a label control instance by creating an instance of the Label class. Here is a JavaFX Label instantiation example: Label label = new Label("My Label"); As you can see, the text to display in the label is passed as parameter to the Label constructor.

How do I change the text on a button click JavaFX?

To make label text change, you need to call setText on the label when you want the text to change. Make your label a member variable for the class, then write: label. setText( Arrays.

How do you make a Label invisible in JavaFX?

Making Labels Invisible You can set the value to this property using the setLabelsVisible() method. To make the labels of the current pie chart invisible you need to invoke this method by passing the boolean value false as a parameter.


1 Answers

Here is a workaround until someone post something better.

If you double click the label it changes to a TextArea. You can then copy the text. Once you press enter on the TextArea it changes back to the label.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication110 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        VBox root = new VBox();

        StackPane stackpane = new StackPane();        

        Label label = new Label("Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world Hello world");
        VBox.setVgrow(label, Priority.ALWAYS);
        label.wrapTextProperty().set(true);

        label.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
                    if(mouseEvent.getClickCount() == 2){
                        label.setVisible(false);
                        TextArea textarea = new TextArea(label.getText());
                        textarea.setPrefHeight(label.getHeight() + 10);
                        stackpane.getChildren().add(textarea);

                        textarea.setOnKeyPressed(event ->{
                            System.out.println(event.getCode());
                            if(event.getCode().toString().equals("ENTER"))
                            {
                                stackpane.getChildren().remove(textarea);
                                label.setVisible(true);                               
                            }
                        });
                    }
                }
            }
        });

        stackpane.getChildren().add(label);   

        root.getChildren().add(stackpane);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}
like image 142
Sedrick Avatar answered Sep 30 '22 15:09

Sedrick