[英]Autosize text area into tabpane in javaFX
I hava 3 tabs in a TabPane that each one has a text area with different texts and different length. 我在TabPane中拥有3个标签,每个标签都有一个带有不同文本和不同长度的文本区域。 I want to autosize text area according to it's length in each tab. 我想根据每个选项卡中的长度自动调整文本区域的大小。 I don't understand what should I do ? 我不知道该怎么办? using scene builder ? 使用场景生成器? css ?javaFX methods ? CSS?javaFX方法? Thank's in Advance ... 提前致谢 ...
I think you are asking that the text areas grow or shrink according to the text that is displayed in them? 我认为您是在要求文本区域根据显示在其中的文本是增长还是缩小?
If so, see if this code helps: 如果是这样,请查看此代码是否有帮助:
import java.util.concurrent.Callable;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AutosizingTextArea extends Application {
@Override
public void start(Stage primaryStage) {
TextArea textArea = new TextArea();
textArea.setMinHeight(24);
textArea.setWrapText(true);
VBox root = new VBox(textArea);
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
// This code can only be executed after the window is shown:
// Perform a lookup for an element with a css class of "text"
// This will give the Node that actually renders the text inside the
// TextArea
Node text = textArea.lookup(".text");
// Bind the preferred height of the text area to the actual height of the text
// This will make the text area the height of the text, plus some padding
// of 20 pixels, as long as that height is between the text area's minHeight
// and maxHeight. The minHeight we set to 24 pixels, the max height will be
// the height of its parent (usually).
textArea.prefHeightProperty().bind(Bindings.createDoubleBinding(new Callable<Double>(){
@Override
public Double call() throws Exception {
return text.getBoundsInLocal().getHeight();
}
}, text.boundsInLocalProperty()).add(20));
}
public static void main(String[] args) {
launch(args);
}
}
If you want to make this reusable, then you could consider subclassing TextArea
. 如果您想使其可重用,则可以考虑将TextArea
子类化。 (In general, I dislike subclassing control classes.) The tricky part here would be to execute the code that makes the TextArea
expand once it has been added to a live scene graph (this is necessary for the lookup to work). (通常,我不喜欢对控件类进行子类化。)这里最棘手的部分是一旦将TextArea
添加到实时场景图中,便执行使TextArea
扩展的代码(这对于查找工作是必需的)。 One way to do this (which is a bit of a hack, imho) is to use an AnimationTimer
to do the lookup, which you can stop once the lookup is successful. 一种执行此操作的方法(有点小技巧,恕我直言)是使用AnimationTimer
进行查找,一旦查找成功,您就可以停止查找。 I mocked this up here . 我在这里嘲笑了这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.