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

can't assign focus to JTextField (Solaris only)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • None
    • 1.4.0
    • client-libs
    • None
    • generic
    • solaris_2.6

      In the program below, the JTextField never gets focus. It is impossible to get focus to it by clicking on it, or by hitting TAB, or in any other way that I know of.

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

      public class FocusTest extends JFrame
      {
        JScrollPane workPane;
        static PSearch panel0, panel1;
        JSplitPane split_V1;
        JPanel emptyPane;
        
        public FocusTest() {
          super("Focus Test");
          
          workPane=new JScrollPane();
          panel0=new PSearch();
          
          emptyPane=new JPanel();
          JLabel lblEmpty=new JLabel("Empty Pane");
          lblEmpty.setFont(new Font("SansSerif",Font.BOLD,18));
          emptyPane.add(lblEmpty);
          
          split_V1=new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel0,emptyPane);
          
          getContentPane().add(split_V1, BorderLayout.CENTER);
          
          setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
          addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
      if (JOptionPane.showConfirmDialog(null,
      "Do you really want to exit?", "Focus Test",
      JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION) {

                  System.exit(0);
      }
            }
            /* public void windowActivated(WindowEvent we) {
      FocusTest.panel0.txtSearch.requestFocus();
            }
            public void windowOpened(WindowEvent we) {
      FocusTest.panel0.txtSearch.requestFocus();
            } */
          });
          
        }

        public static void main(String s[]) {
          FocusTest window = new FocusTest();
          window.setSize(450, 450);
          window.setVisible(true);
        }


        protected class PSearch extends JPanel implements ActionListener {
          protected JTextField txtSearch=new JTextField(10);
          protected JLabel lblSearch=new JLabel("Enter search string: ");
          protected JButton btnSearch=new JButton("Search");
          
          public PSearch() {
            btnSearch.setActionCommand("Search");
            btnSearch.setMnemonic(KeyEvent.VK_S);
            lblSearch.setLabelFor(btnSearch);
            btnSearch.addActionListener(this);

            txtSearch.setActionCommand("Search");
            txtSearch.addActionListener(this);

            txtSearch.addFocusListener(new FocusAdapter(){
      public void focusGained(FocusEvent e){
      txtSearch.selectAll();
      }
            });

            //Layout the components
            add(lblSearch);
            add(txtSearch);
            add(btnSearch);
          }

          public void actionPerformed(ActionEvent e){
            String cmd=e.getActionCommand();
            if(cmd.equals("Search") && FocusTest.panel1==null) {
      this.txtSearch.requestFocus();
      DlgException ex=new DlgException("This is a test message!");
      if (ex.show()==JOptionPane.YES_OPTION) {
      //Do something here, then show another messagebox or dialog.
      ex=new DlgException("Another message...");
      ex.show();
      //Disable the components in panel0 does not help.
      //for(int i=0;i<this.getComponentCount ();i++)
      // this.getComponent(i).setEnabled (false);

      FocusTest.panel1=new PSearch();
      split_V1.setBottomComponent(FocusTest.panel1);
      split_V1.repaint();

      FocusTest.panel1.txtSearch.requestFocus();
      }
            }
          }
        }

        protected class DlgException {
          private String msg=null;
          private String[] options={"Override",
      "Cancel"};
          
          public DlgException(String msg) {
            this.msg =msg;
          }

          public int show(){
            return JOptionPane.showOptionDialog(null,
      msg,
      "Exception message",
      JOptionPane.YES_NO_OPTION,
      JOptionPane.WARNING_MESSAGE,
      null,
      options,
      null );
          }
        }

      }

            hgajewsksunw Hania Gajewska (Inactive)
            hgajewsksunw Hania Gajewska (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: