JTextField inside JPanel, does not insertUpdate properly?
843806Oct 29 2008 — edited Oct 30 2008Hello,
I want to put a JTextField inside a JPanel, and make it resize to accomodate user editing, and I want to do it by validating the layout of the top container on a DocumentEvent.
OK, this works with one glitch, if the first operation is insert, that does not resize. After that everything works, as well as if the first operation is remove, also everything works from the start.
WTF? :)
Here is the code, isolated as much as possible.
import javax.swing.*;
import javax.swing.event.*;
public class Foo extends JFrame
{
Foo()
{
JPanel fooPanel = new JPanel();
add(fooPanel);
fooPanel.add(new TxtField("foo", this));
}
public static void main(String args[])
{
Foo foo = new Foo();
foo.setSize(1024, 1024);
foo.setVisible(true);
}
}
class TxtField extends JTextField implements DocumentListener
{
JFrame mContToValidate;
TxtField(String str, JFrame contToValidate)
{
super(str);
mContToValidate = contToValidate;
getDocument().addDocumentListener(this);
}
public void insertUpdate(DocumentEvent e)
{
mContToValidate.validate();
}
public void removeUpdate(DocumentEvent e)
{
mContToValidate.validate();
}
public void changedUpdate(DocumentEvent e)
{
mContToValidate.validate();
}
}