JDK-4336643 : Change of focus within a window does not select text in JTextField
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_95,windows_nt
  • CPU: x86
  • Submitted: 2000-05-09
  • Updated: 2000-12-07
  • Resolved: 2000-12-07
Related Reports
Duplicate :  
Description

Name: stC104175			Date: 05/09/2000


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


When running in Java 1.2.2 any change of focus between multiple JTextFields in a
window would select the text in the newly focused component. This behavior has
been changed in Java 1.3 (text in JTextFields is no longer selected when the
field is the focused component). Why did this change, and does this mean that we
will now have to write our own focus manager if we want the 1.2.2 behavior?

here is a short sample, that if run in 1.2.2 will demonstrate what I mean.
In 1.2.2 the text in the first JTextField will be selected as soon as the
window becomes visible, and as you tab through the fields, the text in the next
field will be selected. If you run this in 1.3 you have to manually select the
text with the mouse if you want to over write the default text.

/*
 *	This is a little test program to check if the focus
 *	selection mechanism is consistent between Java 1.2.2 and
 *	Java 1.3.
 */

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

/**
 *	Just a simple little window with two JTextFields and a
 *	JComboBox. In Java 1.2.2 if you are using the Windows
 *	look and feel, any text in the JTextFields will be selected
 *	when the focus is on that field. In Java 1.3 rc3 this is not the case.
 *	However, the ComboBox will have a selection background.
 */
public class FocusTest extends JPanel {

	private JTextField nameField;
	private JComboBox rankBox;
	private JTextField serialField;
	
	private String[] comboChoices = { "Private", "Corporal", "Sargent",
					  "Lieutenant", "Captain", "Major" };
	/**
	 *	Constructor
	 */
	public FocusTest() {
	
		super();
		this.setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );
		
		JPanel namePanel = new JPanel( new FlowLayout() );
		JLabel nameLabel = new JLabel( "Enter your name:" );
		nameField = new JTextField( 7 );
		nameField.setText( "foobar" );
                // Uncomment this nextto restore 1.2.2 behavior
//		nameField.selectAll();
		namePanel.add( nameLabel );
		namePanel.add( nameField );
		
		JPanel rankPanel = new JPanel( new FlowLayout() );
		JLabel rankLabel = new JLabel( "Enter your rank:" );
		rankBox = new JComboBox( comboChoices );
		rankBox.setSelectedIndex( 1 );
		rankPanel.add( rankLabel );
		rankPanel.add( rankBox );
		
		JPanel serialPanel = new JPanel( new FlowLayout() );
		JLabel serialLabel = new JLabel( "Enter your serial number:" );
		serialField = new JTextField( 7 );
		serialField.setText( "RA 444 444" );
                // Uncomment this nextto restore 1.2.2 behavior
//		serialField.selectAll();
		serialPanel.add( serialLabel );
		serialPanel.add( serialField );
		
		this.add( namePanel );
		this.add( rankPanel );
		this.add( serialPanel );
		
	}
	
	public static void main( String args[] )
	{
		// Set the look and feel
		try {
			UIManager.setLookAndFeel
			("com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
		}
		catch (Exception e) { }

		// Create aa frame for the panel
		final JFrame f = new JFrame( "Focus Test" );
		f.addWindowListener( new WindowAdapter() {
			public void windowClosed( WindowEvent evt ) {
				System.exit( 0 );
			}
		});
				
		f.setContentPane( new FocusTest() );
		f.pack();
		f.show();
		
	}
	
}
(Review ID: 104598) 
======================================================================

Comments
WORK AROUND Name: stC104175 Date: 05/09/2000 You can add a line like this when you instantiate a JTextField: myJTextField.selectAll(); If you do this to every field in the window, the text in every field will be selected when it gets the focus. ======================================================================
11-06-2004

EVALUATION This has been filed as 4337647, refer to it for more details. scott.violet@eng 2000-12-07
07-12-2000