2014-05-06 51 views
1

我有這個非常簡單的GridPane示例。從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); 

如何在程序運行時用鼠標選擇文本並複製它?

回答

5

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

替代的解決方案

你可以,如果你想使用WebView。對於某些情況可能是更好的解決方案,但對於其他情況則可能不是。

+0

那麼結果不是很好。還有其他解決方案嗎? – user1285928

+2

我不知道「不太好」是什麼意思。編輯您的問題以更準確地解釋,您的要求,願望和進展。 – jewelsea