Skip to main content

Java 7: text field does not update size correctly

3 replies [Last post]
tsinger
Offline
Joined: 2003-06-10
Points: 0

I'm not sure whether following is a bug in Java 7 or caused by some incorrect API usage from me.

When invoking the following code on Java 6, the text field automatically resizes when typing characters. With Java 7, the text field does not resize.

import java.awt.*;<br />
import javax.swing.*;<br />
import javax.swing.event.*;</p>
<p>public class Java7TextFieldSizeTest {</p>
<p>	public static void main(String[] args) {<br />
		SwingUtilities.invokeLater(new Runnable() {<br />
			public void run() {<br />
				final JTextField textField = new JTextField() {<br />
					@Override<br />
					public Dimension getPreferredSize() {<br />
						final Dimension preferredSize = super.getPreferredSize();<br />
						preferredSize.width += 10;<br />
						return preferredSize;<br />
					}<br />
				};</p>
<p>				final JPanel panel = new JPanel(new GridBagLayout());<br />
				panel.add(textField, new GridBagConstraints());</p>
<p>				textField.getDocument().addDocumentListener(new DocumentListener() {<br />
					public void insertUpdate(DocumentEvent e) {<br />
						relayout();<br />
					}</p>
<p>					public void removeUpdate(DocumentEvent e) {<br />
						relayout();<br />
					}</p>
<p>					public void changedUpdate(DocumentEvent e) {<br />
						relayout();<br />
					}</p>
<p>					private void relayout() {<br />
						textField.invalidate();<br />
						panel.validate();<br />
					}<br />
				});</p>
<p>				final JFrame frame = new JFrame("Textfield does not resize correctly");<br />
				frame.setContentPane(panel);<br />
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
				frame.setSize(400, 300);<br />
				frame.setVisible(true);<br />
			}<br />
		});<br />
	}<br />
}

I've tried it on Windows XP Pro SP3 with Java 7 ea b86 (2010-03-18).

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
anthony_p
Offline
Joined: 2006-07-24
Points: 0

Hi tsinger,

Try substituting this:

[code]
textField.invalidate();
panel.validate();
[/code]
with that:

[code]
textField.revalidate();
[/code]

Swing components require using Swing machinery for purposes of validating the component hierarchy. Otherwise, if you require immediate validation and actually have to use the AWT's validate() method, please see the specification of the method as of JDK 7 build 77 or later. The fixes [1] and [2] slightly updated it making the invalidate() method stop invalidating the hierarchy on the first encountered validate root, which probably makes your subsequent panel.validate() call a no-op since the panel is still valid.

[1] http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6852592
[2] http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6868255

--
best regards,
Anthony

tsinger
Offline
Joined: 2003-06-10
Points: 0

Thanks to both of you. Using revalidate seems to have solved the problem.

ru_pashka
Offline
Joined: 2007-04-23
Points: 0

Hi

Because you're using the Swing components you should use the javax.swing.JComponent#revalidate method instead of AWT methods. In your case you should use something like this:

[code]
panel.revalidate();
[/code]

Regards, Pavel