Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

JTextField problems: revalidate ineffective, repaint loop, caret display problems



Encountered some problems with JTextField. Perhaps someone can identify either
my confusion or a workaround... It would also be interesting to know if the same
things happen on other platforms.


(1) If I edit the text in a JTextField, revalidate is called (correctly, I believe, as the
preferred size tracks the current text), but for some reason the layout is not
updated. In my test code, getPreferredSize() is not called.


(2) If I insert a character when the caret is at the right edge of the field, several
things go wrong. (A) If the caret is set to blink, it stops blinking. (B) The focus ring
is overwritten. (C) The JTextField is repainted continuously at a high rate. This
repainting is not the caret blink repainting, as it happens even if the caret is set
to not blink. This problem might be related to a JTextField performance problem
that was reported in Oct 2004 but not confirmed by others.


I have observed these problems in the latest 1.4.2 and the latest DP of 1.5
and with both the Aqua and Metal L&Fs.



package test;

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

/*
	Problems:

If you type a character while the carat is at the right edge of the field,
paint will be called repeatedly and apparently forever at a fast rate,
faster than the blink rate, regardless of whether
the caret is set to blink or not. If the caret is set to blink, it stops
blinking. Deleting the character will restore normal behavior, as will
resizing the window. It also appears
that the focus ring is being overwritten on the right side of the field
when this happens.


	Although revalidate is called when the user edits the text, the layout
	is not redone until the window is resized.

	Using the Metal look and feel does not solve the problems.

	Observed in latest 1.4.2 and 1.5 DP.

*/

public class TestJTextField
{
public static void main(String[] args)
{
System.out.println("Version: " + System.getProperty("java.version"));
boolean blink = (args.length > 0 && args[0].equals("b"));
new TestJTextField(blink);
}


	static boolean debugFlag = true;
	JFrame fr;
	JTextField f;
	int counter;

	public TestJTextField(boolean blink)
	{
		fr = new JFrame();
		f = new MyJTextField("Horse", blink);
		f.setEditable(true);
		fr.getContentPane().add(f);

		GridBagLayout l = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.NONE;
		l.setConstraints(f, c);
		fr.getContentPane().setLayout(l);

		fr.setSize(300, 100);
		fr.setVisible(true);
	}

	private class MyJTextField
		extends JTextField
	{
		public MyJTextField(String s, boolean blink)
		{
			super(s);
			if (!blink) {
				Caret c = getCaret();
				c.setBlinkRate(0);
			}
		}

		public void revalidate()
		{
			if (debugFlag) System.out.println("revalidate");
			super.revalidate();
		}

		public void repaint()
		{
			if (debugFlag) System.out.println("repaint");
			super.repaint();
		}

public void paint(Graphics g)
{
if (debugFlag) System.out.println("paint " + (++counter));
super.paint(g);
}


		public Dimension getPreferredSize()
		{
			if (debugFlag) System.out.println("getPreferredSize");
			return super.getPreferredSize();
		}

		public void setBounds(int x, int y, int w, int h)
		{
			if (debugFlag) System.out.println("setBounds");
			super.setBounds(x, y, w, h);
		}
	}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.