【发布时间】:2021-02-22 09:03:58
【问题描述】:
我在 JavaFX 中使用了一些 TextField,我想限制它们显示文本的区域。我已经限制了它们的最大字符数,但在某些情况下,输入的最大字符数大于文本字段的宽度。如您所见,文本与我添加的自定义擦除文本按钮重叠。
我想知道我是否可以将文本的右边距向左移动一点(不改变 TextField 的属性 - 大小、坐标),这样按钮和文本就不会再重叠了。
我已经尝试过边距、填充,但它们没有达到我的要求。
如何限制我的 TextField 的最大长度(来自 stackoverflow):
public class LimitedJFXTextField extends JFXTextField {
private final IntegerProperty maxLength;
public LimitedJFXTextField() {
super();
this.maxLength = new SimpleIntegerProperty(-1);
}
public IntegerProperty maxLengthProperty() {
return this.maxLength;
}
public final Integer getMaxLength() {
return this.maxLength.getValue();
}
public final void setMaxLength(Integer maxLength) {
Objects.requireNonNull(maxLength,
"Max length cannot be null, -1 for no limit");
this.maxLength.setValue(maxLength);
}
@Override
public void replaceText(int start, int end, String insertedText) {
if (this.getMaxLength() <= 0) {
// Default behavior, in case of no max length
super.replaceText(start, end, insertedText);
} else {
// Get the text in the textfield, before the user enters something
String currentText = this.getText() == null ? "" : this.getText();
// Compute the text that should normally be in the textfield now
String finalText = currentText
.substring(0, start) + insertedText + currentText
.substring(end);
// If the max length is not excedeed
int numberOfexceedingCharacters = finalText.length() - this
.getMaxLength();
if (numberOfexceedingCharacters <= 0) {
// Normal behavior
super.replaceText(start, end, insertedText);
} else {
// Otherwise, cut the the text that was going to be inserted
String cutInsertedText = insertedText.substring(
0,
insertedText.length() - numberOfexceedingCharacters
);
// And replace this text
super.replaceText(start, end, cutInsertedText);
}
}
}
}
【问题讨论】:
-
布局是皮肤的任务:添加子类,子类TextFieldSkin,添加需要的东西并覆盖computeXX和layoutChildren以适应它们。无关:为什么这个手动破解来限制字符数?这是可能的,但属于石器时代,目前实现它的方法是使用 TextFormatter
-
无论如何,无论何时寻求调试帮助,都需要 minimal reproducible example - 而不仅仅是(复制?)代码的 sn-p。实际上,当布局出现问题时,根本不需要其他专业,如字符限制(重复:以非常过时的方式!)
-
@DevilsHnd 不能确定,但是:问题是关于布局,而不是关于限制字段中的字符数(这在您引用的 QA 中得到了很好的回答:)
-
@kleopatra 是的,你是对的,问题是关于布局的。另外,我使用的是手动破解,因为老实说,我不知道 TextFormatter。我第一次尝试使用 TextProperty,但至少在我的实现方式中,它是错误的。然后,我切换到手动石器时代的黑客。谢谢你的帮助!我还没有解决我的问题,即使你给了我一个解决方案,但我会继续努力。至少,我修改了限制字符数的方式:)
-
@kleopatra 你能再具体一点吗?我正在尝试你所说的,在过去的几个小时里,我什至没有一点进展(加上谷歌搜索了 2 天;除了你的建议之外,没有找到相关的东西)。如果我的回复很烦人,或者违反了一些规则,请删除我的帖子。谢谢!
标签: java javafx textfield jfoenix