Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4474373

Doing selectAll in a JTextComponent on focusGained event doesn't work

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs



      Name: bsC130419 Date: 06/25/2001


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



      In previous release of the jdk, doing a selectAll on a JTextField when it
      gained focus with a mouse, was possible and the text remained selected.

      In the jdk 1.4 beta0 version, an extraneous caret event when the mouse is
      released repositions the caret and the selection is unmade.

      Example:
      Run the example program.
      Click with the mouse on the second field.

      Event history;
      field 2: mouse pressed
      field 2: selected all
      field 2: dot=12,mark=0
      field 2: mouse released
      field 2: dot=8,mark=8

      In the jdk 1.3.1 version the this last extraneous caret event doesn't happen
      and the text remains selected.

      If the wait between the mouse down and mouse up events is large, the original
      selectAll caret event is discarded and only the extraneous event happens
      (coalescing events?).

      Example program:

      /* TestBug3.java */

      import java.awt.Component;
      import java.awt.FlowLayout;
      import java.awt.event.FocusAdapter;
      import java.awt.event.FocusEvent;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;
      import javax.swing.JFrame;
      import javax.swing.JTextField;
      import javax.swing.event.CaretEvent;
      import javax.swing.event.CaretListener;


      public class TestBug3 extends JFrame {

      public TestBug3() {

      JTextField field1 = new JTextField("Text field 1");
      field1.setName("field 1");
      field1.addFocusListener(new TextFieldFocusListener());
      field1.addMouseListener(new TextFieldMouseListener());
      field1.addCaretListener(new TextFieldCaretListener());

      JTextField field2 = new JTextField("Text field 2");
      field2.setName("field 2");
      field2.addFocusListener(new TextFieldFocusListener());
      field2.addMouseListener(new TextFieldMouseListener());
      field2.addCaretListener(new TextFieldCaretListener());

      getContentPane().setLayout(new FlowLayout());
      getContentPane().add(field1);
      getContentPane().add(field2);

      }

      private class TextFieldFocusListener extends FocusAdapter {

      public void focusGained(FocusEvent event) {
      JTextField field = (JTextField) event.getComponent();
      System.out.println(field.getName() + ": selected all");
      field.selectAll();
      }
      }

      private class TextFieldMouseListener extends MouseAdapter {

      public void mousePressed(MouseEvent e) {
      System.out.println(e.getComponent().getName() + ":
      mouse pressed");
      }

      public void mouseReleased(MouseEvent e) {
      System.out.println(e.getComponent().getName() + ":
      mouse released");
      }
      }

      private class TextFieldCaretListener implements CaretListener {

      public void caretUpdate(CaretEvent e) {
      System.out.println(((Component) e.getSource()).getName
      () + ": " + e);
      }
      }

      public static void main(String[] args) {
      TestBug3 test4 = new TestBug3();
      test4.setDefaultCloseOperation(EXIT_ON_CLOSE);
      test4.pack();
      test4.setLocation(100, 50);
      test4.setVisible(true);
      }
      }
      (Review ID: 127248)
      ======================================================================

            shickeysunw Shannon Hickey (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: