2017-05-25 14 views
2

JavaFx8でLabelテキストを選択できるようにする方法はありますか? TextFieldのような簡単な回避策があります。しかし、私のラベルには、TextFieldが提供しないラッピング機能を持つ複数行のテキストが必要です。 TextAreaを使用している場合、ラベルのようなテキストのサイズに基づいてTextAreaを縮小できないという問題があります。だから私はどちらも使うことができない。Javafxラベルを選択する方法

また、ラベルテキストの私の使用は、以下のようなものです:VBoxの幅に応じて、

<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> 

、ラベルの高さは完全にテキストに合わせてサイズを変更します。 TextAreaまたはTextFieldを使用して、この種の動作をエミュレートすることはできません。しかし、私はラベルからテキストを選択できる必要があります。何か案は?

+0

私はそれが可能だとは思わない、またはそれが要求された機能だように、少なくともそれは、Oracle suggests.Looksの何[この](https://community.oracle.com/thread/2319231)スレッドです将来のリリースで実装される予定です。 –

+0

オープンソースのカスタムコントロールは誰でも作成できますか? –

+0

これを試してみてください:https://stackoverflow.com/questions/22534067/copiable-label-textfield-labeledtext-in-javafx –

答えて

4

誰かが何か良いものを投稿するまで回避策があります。

ラベルをダブルクリックすると、TextAreaに変わります。テキストをコピーすることができます。 TextAreaでenterを押すと、ラベルに戻ります。

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

} 
+0

これは賢明な回避策です、私はそれが好きです。 –

関連する問題