Search code examples
javaswingjtextfieldjformattedtextfield

lostFocusEvent not occuring when I delete text inside a Jtextfield?


Hi I have implemented a simple focusListener method and registered two jTextFields to it. What this method does is to add the numbers in them and display it in a JLabel. If I typed "2" in them, it will correctly update to give me 4. However, if I just delete the 2 afterwards, there will be no focusLost event being fired even if I click elsewhere. If I enter 0 in the JTextField, then the focusLost event will occur normally. Why is that so? Thanks!


Solution

  • Without THE SSCCE, it's hard to say the logic you are using in your case. Since in this example that follows, it's working as you are expecting :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class TextFieldExample
    {
        private JTextField tfield1;
        private JTextField tfield2;
        private JLabel label;
        private FocusListener tfieldListener = new FocusListener()
        {
            @Override
            public void focusGained(FocusEvent fe)
            {
            }
    
            @Override
            public void focusLost(FocusEvent fe)
            {
                String num1 = tfield1.getText().trim();
                String num2 = tfield2.getText().trim();
                if (num1 == null || num1.equals(""))
                    num1 = "0";
                if (num2 == null || num2.equals(""))
                    num2 = "0";         
                label.setText(Integer.toString(Integer.parseInt(num1) + Integer.parseInt(num2)));
            }
        };
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Text Field Focus Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    
            tfield1 = new JTextField(10);
            tfield2 = new JTextField(10);
    
            tfield1.addFocusListener(tfieldListener);
            tfield2.addFocusListener(tfieldListener);
    
            ((AbstractDocument)tfield1.getDocument()).setDocumentFilter(new MyDocumentFilter());
            ((AbstractDocument)tfield2.getDocument()).setDocumentFilter(new MyDocumentFilter());
    
            label = new JLabel("SUM IS");
    
            contentPane.add(tfield1);
            contentPane.add(tfield2);
            contentPane.add(label);
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        class MyDocumentFilter extends DocumentFilter
        {
            @Override
            public void insertString(FilterBypass fb, int offset
                                                    , String text
                                                    , AttributeSet aset)
            {
                try
                {
                    super.insertString(fb, offset, text.replaceAll("\\D++", ""), aset);
                }
                catch(BadLocationException ble)
                {
                    ble.printStackTrace();
                }
            }
    
            @Override
            public void replace(FilterBypass fb, int offset, int len
                                               , String text
                                               , AttributeSet aset)
            {
                try
                {
                    super.replace(fb, offset, len, text.replaceAll("\\D++", ""), aset);
                }
                catch(BadLocationException ble)
                {
                    ble.printStackTrace();
                }
            }       
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TextFieldExample().displayGUI();
                }
            });
        }
    }