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

double-click on disabled JTextField causes other textfield to select content

XMLWordPrintable

    • beta
    • x86
    • windows_nt



      Name: dbT83986 Date: 01/14/99


      If you double-click on a disabled JTextField, the contents of
      the last focussed JTextField are selected.

      BeginWordAction is called with Event==null, which causes the
      subsequent TextAction.getTextComponent() to return not the
      component from the event (which is null), but to call
      getFocusedComponent() which returns the last TextComponent,
      which had focus before. Which in the example is "Field 1".


      --------------------------------------
      o The program shows two JTextFields and a JButton.
      o The button "Toggle Enabled" toggles setEnabled() on both buttons.
      o Press the button, now you can input something in both fields.
      o Input something in the upper textfield.
      o Press the button again, now both fields are disabled.
      o If you now double-click the lower (still empty) textfield,
        the content of the upper textfield gets selected.

      import javax.swing.*;
      import javax.swing.text.*;

      import java.awt.*;
      import java.awt.event.*;
      import java.util.Vector;

      public class T02
      implements ActionListener
      {

      JTextField tf1, tf2;
      JButton b;

          public T02()
      {
              JFrame frame = new JFrame("B038.T02");

              frame.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
      System.out.println("Window closing.");
      System.exit(0);}});

      Container cont = frame.getContentPane();
      cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));

      tf1 = new JTextField(20);
      tf1.setEnabled(false);
      tf1.setName("Field 1");
      cont.add(tf1);

      tf2 = new JTextField(20);
      tf2.setEnabled(false);
      tf2.setName("Field 2");
      cont.add(tf2);

      b = new JButton("Toggle Enabled");
      cont.add(b);
      b.addActionListener(this);

              frame.pack();
              frame.setVisible(true);
          }
          
          public static void main(String[] args)
      {
      try {
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      } catch (Exception ex) {
      System.out.println("cannot set LookAndFeel: "+ex);
      //System.exit(0);
      }
              new T02();
          }

      public void actionPerformed(ActionEvent ev)
      {
      if (ev.getSource() == b) {
      boolean b = tf1.isEnabled();
      tf1.setEnabled(! b);
      tf2.setEnabled(! b);
      }
      }
      }
      (Review ID: 52529)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: