How to choose a Javafx shortcut

Is there a way to make the label text available in JavaFx8? I know there are other easy workarounds like using a TextField. But my shortcut needs multi-line text with a wrapper that TextField does not provide. If I use a TextArea, the problem is that I cannot shrink the TextArea based on the size of the text like Label. Therefore, I cannot use any of them.

Also my use of label text looks like this:

<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 width of the VBox, the height of the labels changes to match the text perfectly. I cannot emulate this kind of behavior using TextArea or TextField. But I need to select text from the label. Any ideas?

+3


source to share


1 answer


Here's a workaround until someone posts something better.

If you double click on the label it will change to TextArea. Then you can copy the text. When you hit Enter on the TextArea it will revert 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);
    }

}

      

+5


source







All Articles