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