Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4740914
Votes 0
Synopsis Doing selectAll() in a JFormattedTextField on focusGained event doesn't work
Category java:classes_swing
Reported Against 1.4 , hopper-rc
Release Fixed
State 11-Closed, Will Not Fix, bug
Priority: 3-Medium
Related Bugs
Submit Date 03-SEP-2002
Description




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 :
 customer  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) 
======================================================================
Work Around
N/A
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.
  xxxxx@xxxxx   2002-09-03
Comments
  
  Include a link with my name & email   

Submitted On 21-APR-2003
soulguru
extend the JFormattedTextfield and override the
method processFocusEvent like this:

    protected void processFocusEvent(FocusEvent e) 
    {
        super.processFocusEvent(e);
        if ( e.getID() == FocusEvent.FOCUS_GAINED )        
  selectAll() ;
    }


Submitted On 09-FEB-2004
krick-zero
THANK YOU, soulguru.
Your fix works like a charm.


Submitted On 20-JUL-2007
Or do a:

SwingUtilities.invokeLater(new Runnable() {
	@Override
	public void run() {
		textComponent.selectAll();
	}
});



Submitted On 12-JUN-2008
Please note that this solution does not work when this component is used as an editor in a JTable with surrender-focus enabled.



PLEASE NOTE: JDK6 is formerly known as Project Mustang