在JavaFX(行/行)中使用TextHeight使TextArea增长

时间:2023-02-08 17:03:36

I have been trying to make the TextArea grow with its content, for exmaple:

我一直试图让TextArea增加其内容,例如:

There is one Line in the Text area, now the user keeps writing and reaches the TextArea's right border and the text wraps, now he does have a second line (which is not wrapped by simply using '\n' internally) and I now want the text field to grow for the height of one more line.

文本区域中有一行,现在用户继续写入并到达TextArea的右边框并且文本换行,现在他确实有第二行(内部没有简单地使用'\ n'包裹)我现在想要文本字段增加一行的高度。

What I have tried already:

我已经尝试过了:

This

Text text = textArea.lookup(".text").getLocalBounds.getHeight()

always returns the same, no matter how much Lines i do have.

无论我有多少行,总是返回相同的。

This

textArea.getPrefRowCount()

always returns

1

no matter how much lines I have.

无论我有多少台线。

How can I achieve this? If someone wants a working example, Skype has that kind of mechanism in its chat.

我怎样才能做到这一点?如果有人想要一个有效的例子,Skype在聊天中就有这种机制。

2 个解决方案

#1


0  

This

Text text = (Text) textArea.lookup(".text");
textArea.setPrefHeight(text.boundsInParentProperty().get().getMaxY());

works but is not very nice since the scroll bars keep bugging.

有效,但不是很好,因为滚动条保持窃听。

#2


-1  

Should not be too hard to do it. I think you will probably have to @Override the setPreferredRowCount() method of textArea to return the number of rows based on what you need, or on a fixed value.

不应该太难做到这一点。我想你可能需要@Override textArea的setPreferredRowCount()方法来根据你需要或固定值返回行数。

This is a common "issue" u can encounter on any Swing component such as JFrame, JPanel, JLabel, JButton, etc.

这是您在任何Swing组件上遇到的常见“问题”,例如JFrame,JPanel,JLabel,JButton等。

Also on these components too it is highly recommended to override the preferred methods in order to correctly resize them, instead of using setBounds.

此外,对于这些组件,强烈建议覆盖首选方法以正确调整它们的大小,而不是使用setBounds。

Also maybe the setPreferredRowCount() method won't update herself so you would probably have to call it in a loop (or a Thread but I'd rather not) to make sure your sizes are always updated. Something like:

也许setPreferredRowCount()方法不会更新自己,所以你可能不得不在一个循环(或一个线程,但我宁愿不)中调用它,以确保您的大小始终更新。就像是:

EDIT:

while(inputNeeded) {
    if(textArea1.textChanged) {
        if(text1.length > 10) {
            textArea1.setPreferredRowCount(3);
        } else if(text1.length > 20) {
            textArea1.setPreferredRowCount(4);
        }
    }
}

... and so on. My bad, you don't really have to override anything. Just try to use a loop like this.

... 等等。我的坏,你真的不必覆盖任何东西。试着使用像这样的循环。

#1


0  

This

Text text = (Text) textArea.lookup(".text");
textArea.setPrefHeight(text.boundsInParentProperty().get().getMaxY());

works but is not very nice since the scroll bars keep bugging.

有效,但不是很好,因为滚动条保持窃听。

#2


-1  

Should not be too hard to do it. I think you will probably have to @Override the setPreferredRowCount() method of textArea to return the number of rows based on what you need, or on a fixed value.

不应该太难做到这一点。我想你可能需要@Override textArea的setPreferredRowCount()方法来根据你需要或固定值返回行数。

This is a common "issue" u can encounter on any Swing component such as JFrame, JPanel, JLabel, JButton, etc.

这是您在任何Swing组件上遇到的常见“问题”,例如JFrame,JPanel,JLabel,JButton等。

Also on these components too it is highly recommended to override the preferred methods in order to correctly resize them, instead of using setBounds.

此外,对于这些组件,强烈建议覆盖首选方法以正确调整它们的大小,而不是使用setBounds。

Also maybe the setPreferredRowCount() method won't update herself so you would probably have to call it in a loop (or a Thread but I'd rather not) to make sure your sizes are always updated. Something like:

也许setPreferredRowCount()方法不会更新自己,所以你可能不得不在一个循环(或一个线程,但我宁愿不)中调用它,以确保您的大小始终更新。就像是:

EDIT:

while(inputNeeded) {
    if(textArea1.textChanged) {
        if(text1.length > 10) {
            textArea1.setPreferredRowCount(3);
        } else if(text1.length > 20) {
            textArea1.setPreferredRowCount(4);
        }
    }
}

... and so on. My bad, you don't really have to override anything. Just try to use a loop like this.

... 等等。我的坏,你真的不必覆盖任何东西。试着使用像这样的循环。