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

requestFocus() does not appear to work

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.1.3, 1.1.4
    • client-libs
    • generic, x86
    • solaris_2.5.1, windows_nt


      Setting requestFocus() does not appear to work. Tried setting focus
      on a textfield and a button. Neither worked.



      // Example of two textfields and a button that when pressed
      // gets the data from the textfields and displays in stdout.
      // Verify that requestFocus does not work. Try setting request-
      // Focus on myTextField2.

      import com.sun.java.swing.*;
      import java.awt.*;
      import java.awt.event.ActionListener;
      import java.awt.event.ActionEvent;
      import java.awt.event.FocusListener;
      import java.awt.event.FocusEvent;

      public class testFocus extends JFrame
      implements FocusListener,
      ActionListener {

          JTextField myTextField = new JTextField(20);
          JTextField myTextField2 = new JTextField(20);
          JButton myButton = new JButton("button");

          public testFocus() {
      JPanel bottomPanel = new JPanel();
      setLayout(new BorderLayout());

      bottomPanel.add(myTextField);
      bottomPanel.add(myTextField2);
      add("Center",bottomPanel);
      add("South", myButton);

      myButton.addActionListener(this);
      myTextField.addFocusListener(this);
      myTextField2.addFocusListener(this);
      myButton.addFocusListener(this);

      //myButton.requestFocus();
      myTextField2.requestFocus();
          }

          public static void main(String args[]) {
      testFocus myWindow = new testFocus();
      myWindow.setTitle("My JTextField Example");
      myWindow.pack();
      myWindow.show();
          }

          public void actionPerformed(ActionEvent evt) {
      if (evt.getSource() == myButton) {
      // Get the contents of myTextField.
      String data1 = myTextField.getText();
      System.out.println("myTextField = " + data1);

      // Get the contents of myTextField2.
      String data2 = myTextField2.getText();
      System.out.println("myTextField2 = " + data2);
      }
          }

          public void focusGained(FocusEvent e) {
      System.out.println("focus gained: " + e.getSource() + "\n");
          }

          public void focusLost(FocusEvent e) {
      System.out.println("focus lost: " + e.getSource() + "\n");
          }
      }

            tsantossunw Tom Santos (Inactive)
            cahorton Catherine Horton
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: