JavaFX:TextArea自动扩展高度而无需滚动条 [英] JavaFX: TextArea automatic height expanding without scrollbar

查看:794
本文介绍了JavaFX:TextArea自动扩展高度而无需滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以强制 TextArea 控件自动扩展高度吗?

在以下情况下,我想在 ScrollPane 控件上看到滚动条,而不是在 TextArea 控件上看到滚动条.

In the following case, I would like to see the scrollbar at ScrollPane control, not at TextArea control.

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>

<ScrollPane fitToHeight="true" fitToWidth="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
            minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.91" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="sample.Controller">
    <VBox style="-fx-background-color: bisque">
        <TextField/>
        <TextArea VBox.vgrow="ALWAYS">
            <VBox.margin>
                <Insets top="20.0"/>
            </VBox.margin>
        </TextArea>
    </VBox>
</ScrollPane>

推荐答案

目前,与您的问题最接近的唯一解决方案是由 @Uluk Biy 提供的,我发现

For now the only solution that is close to your problem is given by @Uluk Biy, that i found here, what i did is just fit his logic, and hide the ScrollBars. The only problem is the size of the TextArea which is binded to that of the Text and so it starts with a minimum height at the beginning of the edition, here is the complete code :

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Launcher extends Application{

private Pane root = new Pane();
private Scene scene;

private ScrollPane scroller;
private Pane content = new Pane();

private TextField textF = new TextField();
private TextArea textA = new TextArea();

private Text textHolder = new Text(); 
private double oldHeight = 0;


@Override
public void start(Stage stage) throws Exception {

    root.getChildren().addAll(yourSP());
    scene = new Scene(root,300,316);
    stage.setScene(scene);
    stage.show();

}

private ScrollPane yourSP(){

    content.setMinSize(300, 300); 
    textF.setPrefSize(260, 40); 
    textF.setLayoutX(20);
    textF.setLayoutY(20); 
    textA.setPrefSize(260, 200);
    textA.setLayoutX(20);
    textA.setLayoutY(80); 

    textA.getStylesheets().add(getClass().getResource("texta.css").toExternalForm());
    content.getChildren().addAll(textA,textF);

    /*************************@Uluk Biy Code**************************/
    textA.setWrapText(true);
    textHolder.textProperty().bind(textA.textProperty());
    textHolder.layoutBoundsProperty().addListener(new ChangeListener<Bounds>() {
        @Override
        public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) {
            if (oldHeight != newValue.getHeight()) {
                oldHeight = newValue.getHeight();
                textA.setPrefHeight(textHolder.getLayoutBounds().getHeight() + 20); 
                System.out.println(textHolder.getLayoutBounds().getHeight());
            }
        }
    });
    /****************************************************************/

    scroller = new ScrollPane(content);
    scroller.setHbarPolicy(ScrollBarPolicy.NEVER); 
    scroller.setPrefSize(300, 316); 


    return scroller;
}


public static void main(String[] args) {

    launch(args); 

  }

 }

当然,代码可以适应fxml格式,我只是没有足够的时间来做,而这就是TextArea的样式:

Of course the code can be adapted to fxml format, I just have not had enough time to do it, and here is the style of the TextArea:

.text-area > .scroll-pane{

-fx-hbar-policy:never;
-fx-vbar-policy:never; 

}

祝你好运!

这篇关于JavaFX:TextArea自动扩展高度而无需滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆