Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTextField inside JPanel, does not insertUpdate properly?

843806Oct 29 2008 — edited Oct 30 2008
Hello,

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();
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 27 2008
Added on Oct 29 2008
5 comments
53 views