从GridPane中选择文本

use*_*928 1 javafx javafx-2 javafx-8

我有一个非常简单的GridPane示例.

    GridPane playerGrid = new GridPane();

    Text title = new Text("Top Scorers in English Premier League");
    title.setFont(Font.font("Arial", FontWeight.BOLD, 20));
    playerGrid.add(title, 0,0,4,1);
Run Code Online (Sandbox Code Playgroud)

如何用鼠标选择文本并在程序运行时复制它?

jew*_*sea 6

JavaFX中的文本节点不可选.

如果要选择文本,请使用选择感知控件.

文本最终放在GridPane中的事实与此问题无关.

selectabletext

例如,使用只读TextField:

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

public class SelectableTextSample extends Application {
    @Override public void start(final Stage stage) throws Exception {
        stage.setScene(
            new Scene(
                new SelectableText(
                    "Top Scorers in English Premier League"
                )
            )
        );
        stage.show();
    }

    class SelectableText extends TextField {
        SelectableText(String text) {
            super(text);
            setEditable(false);
            setPrefColumnCount(20);
        }
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

替代解决方案

如果您愿意,可以使用WebView.对于某些可能是更好解决方案的情况,但对于其他情况则可能不是.

  • 我不知道"不太好"意味着什么.编辑您的问题,以更准确地解释,您的要求,愿望和进步. (2认同)