2016-03-31 17 views
0

私のラベル(lbl)にElipsis String(...)を保持しようとしていますが、ウィンドウが小さくなりすぎると空のStringに設定されます。私は例を示すためにここにコードを使用しています。私はここにあるResizeHelperクラスを使用しています:Allow user to resize an undecorated StageJavaFXラベルに唯一のテキストが表示されている場合、省略記号文字列を空の文字列に設定する方法

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.layout.AnchorPane; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

/** 
* 
* @author bparmeter 
*/ 
public class NewFXMain extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     Label lbl = new Label(); 
     lbl.setText("Hello World"); 
     AnchorPane root = new AnchorPane(); 
     AnchorPane.setBottomAnchor(lbl, 0.0); 
     AnchorPane.setTopAnchor(lbl, 0.0); 
     AnchorPane.setRightAnchor(lbl, 0.0); 
     AnchorPane.setLeftAnchor(lbl, 0.0); 
     root.getChildren().add(lbl); 
     Scene scene = new Scene(root, 300, 250); 
     primaryStage.setScene(scene); 
     primaryStage.initStyle(StageStyle.UNDECORATED); 
     primaryStage.show(); 
     ResizeHelper.addResizeListener(primaryStage); 

    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 
} 

答えて

1

ない最善の解決策が、それは動作します...

lbl.widthProperty().addListener((ov, o, n) -> { 
     final Text t = new Text(lbl.getText().charAt(0)+"..."); 
     t.applyCss(); 
     final double width = t.getLayoutBounds().getWidth(); 
     lbl.setEllipsisString(width>=lbl.getWidth() ? "" : "..."); 
    }); 
関連する問題