4 Replies Latest reply: Oct 22, 2012 3:40 AM by 802908 RSS

    focusLost right after focusGained

    802908
      Hello,

      i am trying to get the focus into a specific JTextField
       
      abbrechenButton.addActionListener(
               new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                           ResetInputfields();
                           tabbedPane1.setSelectedComponent(HauptseitePanel);
                           PVNumber.setText("");
                           boolean Bolle = PVNumber.requestFocusInWindow(); // PVNumber should receive the focus
                           Log.info("(%) requestFocus = "+Bolle);
                    }
               }
              );
      In most cases it works, but in one case it doesn't: PVNumber receives its focusGained event (good), but the focus is lost immediately after (the focusLost event occurs right after focusGained).
      The boolean flag returns true. I use a JTabbedPane with several panels and switch between these panels.

      I have also looked at getOppositeComponent(), the component which "steals" the focus is a JTextPane, but i never call requestFocus on this component.

      thanks for reading

      BB
        • 1. Re: focusLost right after focusGained
          abillconsl
          What is the one case it does not work? Did you place print statements in all listener methods and print values, components, etc?
          • 2. Re: focusLost right after focusGained
            802908
            Hello,

            the button above is on a panel B, i want to display the panel A (HauptseitePanel) and put the focus on the JTextField (on panel A).
            If i just press the button, the focus works correctly.

            19.10.2012 09:27:30 Remote.Main$52 actionPerformed
            INFO: (%) requestFocus = true
            19.10.2012 09:27:30 Remote.Main$51 focusGained
            INFO: FOCUS gained... hooray! + false + opp = javax.swing.JTextPane

            But when i enter something in another JTextField on panel B, and press the button afterwards, the focus gets lost.
            The component, which "steals" the focus, is a component (JTextPane) on panel A.

            19.10.2012 09:28:45 Remote.Main$52 actionPerformed
            INFO: (%) requestFocus = true
            19.10.2012 09:28:46 Remote.Main$51 focusGained
            INFO: FOCUS gained... hooray! + false + opp = javax.swing.JTextPane
            19.10.2012 09:28:46 Remote.Main$51 focusLost
            INFO: FOCUS lost... sorry! + false + opp = javax.swing.JTextPane
             
              PVNumber.addFocusListener(
                      new FocusListener() {
                          public void focusGained(FocusEvent e) {
                                   Log.info("FOCUS gained... hooray! + "+e.isTemporary()+" + opp = "+e.getOppositeComponent().toString() );
                          }
                          public void focusLost(FocusEvent e) {
                                  Log.info("FOCUS lost... sorry! + "+e.isTemporary() +" + opp = "+e.getOppositeComponent().toString() );   
                          }
                      }  
            kind regards

            BB
            • 3. Re: focusLost right after focusGained
              802908
              PS: if i just set the cursor into the JTextField on panel B, and press the button -> ok (focus is correctly set)
              but if i type a number (or text) into the JTextField on panel B, and press the button -> focus is lost...

              I really don't understand these dependencies.
              • 4. Re: focusLost right after focusGained
                802908
                Hi

                i solved the problem by using a ComponentListener
                HauptseitePanel.addComponentListener(new ComponentAdapter() {
                @Override
                public void componentShown(ComponentEvent e) {
                PVNumber.requestFocusInWindow();
                }
                });


                thanks

                BB