I would like to dynamically resize a JTextField
and also set a minimum amount of text allowed.
我想动态调整JTextField的大小,并设置允许的最小文本量。
Example
The text field can have a minimum length of 4, if the text is less than 4 add 0 for every slot missing.
文本字段的最小长度为4,如果文本小于4,则每缺少一个插槽添加0。
1 个解决方案
#1
0
Below there is an example of such JTextField:
下面是一个这样的JTextField的例子:
public class TextField extends JTextField {
public static final int TEXT_FIELD_PADDING = 4;
public TextField() {
addAutoResize();
init();
}
public void init() {
setStyleByParentStyle();
this.setPreferredSize(new Dimension(Constant.
TEXT_FIELD_MINIMAL_WIDTH, this.getPreferredSize().height));
}
private void addAutoResize() {
this.getDocument().addDocumentListener
(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
checkResize();
}
@Override
public void removeUpdate(DocumentEvent e) {
checkResize();
}
@Override
public void changedUpdate(DocumentEvent e) {
checkResize();
}
});
}
private void checkResize() {
FontMetrics fontMetrics = this.getFontMetrics(this.
getFont());
if (fontMetrics.stringWidth(this.getText()) +
TEXT_FIELD_PADDING * 2 < Constant.TEXT_FIELD_MINIMAL_WIDTH) {
this.setPreferredSize(new
Dimension(Constant.TEXT_FIELD_MINIMAL_WIDTH, this.getHeight()));
this.getParent().revalidate();
} else {
//NOTE 2 - border width*2
this.setPreferredSize(new
Dimension(fontMetrics.stringWidth(this.getText()) + 2 +
Constant.CARRET_WIDTH + TEXT_FIELD_PADDING * 2, this.getHeight()));
this.getParent().revalidate();
}
}
}
This information is taken from the article: Frequently Asked Questions during Java applet development
此信息来自文章:Java applet开发期间的常见问题
#1
0
Below there is an example of such JTextField:
下面是一个这样的JTextField的例子:
public class TextField extends JTextField {
public static final int TEXT_FIELD_PADDING = 4;
public TextField() {
addAutoResize();
init();
}
public void init() {
setStyleByParentStyle();
this.setPreferredSize(new Dimension(Constant.
TEXT_FIELD_MINIMAL_WIDTH, this.getPreferredSize().height));
}
private void addAutoResize() {
this.getDocument().addDocumentListener
(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
checkResize();
}
@Override
public void removeUpdate(DocumentEvent e) {
checkResize();
}
@Override
public void changedUpdate(DocumentEvent e) {
checkResize();
}
});
}
private void checkResize() {
FontMetrics fontMetrics = this.getFontMetrics(this.
getFont());
if (fontMetrics.stringWidth(this.getText()) +
TEXT_FIELD_PADDING * 2 < Constant.TEXT_FIELD_MINIMAL_WIDTH) {
this.setPreferredSize(new
Dimension(Constant.TEXT_FIELD_MINIMAL_WIDTH, this.getHeight()));
this.getParent().revalidate();
} else {
//NOTE 2 - border width*2
this.setPreferredSize(new
Dimension(fontMetrics.stringWidth(this.getText()) + 2 +
Constant.CARRET_WIDTH + TEXT_FIELD_PADDING * 2, this.getHeight()));
this.getParent().revalidate();
}
}
}
This information is taken from the article: Frequently Asked Questions during Java applet development
此信息来自文章:Java applet开发期间的常见问题