如何使 Javafx 标签可选

2022-01-25 00:00:00 label css java javafx

在 JavaFx8 中是否可以选择标签文本?我知道,还有其他简单的解决方法,例如使用 TextField.但是我的标签需要带有 TextField 不提供的换行功能的多行文本.如果我使用 TextArea,问题是我不能像标签一样根据文本的大小缩小 TextArea.所以我不能使用它们中的任何一个.

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>

根据 VBox 的宽度,Label 的高度会调整大小以完全适合文本.我无法使用 TextArea 或 TextField 模拟这种行为.但我需要能够从标签中选择文本.有什么想法吗?

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?

推荐答案

这是一个解决方法,直到有人发布更好的东西.

Here is a workaround until someone post something better.

如果您双击标签,它会变为 TextArea.然后,您可以复制文本.在 TextArea 上按 Enter 后,它会变回标签.

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);
    }

}

相关文章