JDK-4740914 : Doing selectAll() in a JFormattedTextField on focusGained event doesn't work
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0,1.4.1
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-09-03
  • Updated: 2002-09-03
  • Resolved: 2002-09-03
Description

Name: jk109818			Date: 09/03/2002


FULL PRODUCT VERSION :
java version "1.4.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_01-b03)
Java HotSpot(TM) Client VM (build 1.4.0_01-b03, mixed mode)

AND

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

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]
Service Pack 2

A DESCRIPTION OF THE PROBLEM :
selectAll() does not work within a focusGained event. It
does by putting the JFormattedTextField in PERSIST focus
lost mode but when choosing other types and focus is lost
the cursor gets moved to the left side. The selectAll in
focusGained will not select the text.

I did a work around for this by invoking later the selectAll
().  I don't know if this is the most elegant or efficient
solutions but it gets the job done.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. add focusGained to the JFormattedTextField. Put the text
field in any state but PERSIST for focus lost.
2. tab in and out of this field.  This eleminates the mouse
problem.
3. witness the problem of not selecting the text.

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expecting to see the selected text.

Actually nothing was selected.

---------------------BEGIN SOURCE---------------------------

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

public class JAutoSelect extends JFrame {

    
    public JAutoSelect() {
	initComponents();
    }

 
    private void initComponents() {
        
	getContentPane().setLayout(new java.awt.FlowLayout());
        
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        
	
        
        JTextField text = new JTextField();
        text.setText("JTextField");
        text.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e){
                JTextField text = (JTextField) e.getSource();
                text.selectAll();
            }
            public void focusLost(FocusEvent e){
            }
        });
        getContentPane().add(text);
        
        JFormattedTextField ftext = new JFormattedTextField("JFormattedTextField");
        ftext.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e){
                JTextField ftext = (JTextField) e.getSource();		   
                ftext.selectAll();
            }
            public void focusLost(FocusEvent e){
            }
        });
        getContentPane().add(ftext);

	setLocation(150,200);
        setSize(400,200);
        pack();
    }

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        new JAutoSelect().show();
    }


}



---------------------END SOURCE---------------------------



REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER WORKAROUND :
Invoking later the selectAll() in a runnable thread.

addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent
evt) {
           SwingUtilities.invokeLater(new Runnable() {
                 public void run() {
                      mySelf.selectAll();
                 }
            });
     }
});
(Review ID: 163841) 
======================================================================

Comments
EVALUATION JFormattedTextField allows for the text to be formatted differently when it has focus and when it doesn't have focus. In order to support this JFTF reformats the text when focus is lost/gained. As the text is completely replaced selectAll has no effect. ###@###.### 2002-09-03
03-09-2002