JAVAFX Make dynamic textArea size

梦想的初衷 提交于 2020-01-15 02:41:05

问题


I'm making chat application using JAVAFX. The messages displayed in textArea, but the textArea always in the same size. How can I make the textArea to fit exactly to the amount of text? TNX


回答1:


The following code does exactly what You wants

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {

        AnchorPane root = new AnchorPane();
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());


        TextArea ta=new TextArea(); 
        ta.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                 // your algorithm to change height
                 ta.setPrefHeight(ta.getPrefHeight()+10); 
            }
        });
        root.getChildren().add(ta);

        primaryStage.setScene(scene);
        primaryStage.show();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}


来源:https://stackoverflow.com/questions/26323716/javafx-make-dynamic-textarea-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!