Resize JavaFX Label if overrun

后端 未结 4 1374
情深已故
情深已故 2020-12-11 11:28

I have a Label in a GridPane in a TitledPane. I want it to downsize stepwise by 0.05em if it is overrun so the three dots (\"Long Labe...\") dont show up -> \"Long Label\" i

4条回答
  •  一向
    一向 (楼主)
    2020-12-11 11:42

    Solution:

    fontVerkleinerung(lblXYZ);   //call resizing at beginning
    
    
     lblXYZ.styleProperty().addListener((observable, oldV, newV) -> {
        fontVerkleinerung(lblXYZ);   //check size again if resized
     }); 
    
    
    private void fontVerkleinerung(Label label) {
        Platform.runLater(() -> {
            tpBounds = tPane.getBoundsInLocal();
            if (label.getBoundsInLocal().getWidth()>tpBounds.getWidth() && !fontSizeFits) {
                fontSize = fontSize-0.02;
                label.setStyle("-fx-font-size: "+fontSize+"em;");
            }
    
            if (label.getBoundsInLocal().getWidth()<=tpBounds.getWidth() && !fontSizeFits) {
                fontSizeFits = true;
            }
        });
    }
    

    This workaround is not 100% satisfying, the GUI is suffering due to the slow graphic updaterate on label.setStyle("...");
    It takes a few ms till the styleProperty-listener kicks in.
    I tried to fix this for a few weeks now, always found a probably-solution but it didn't work. I hope somebody profits from this post.

提交回复
热议问题