0

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!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Laughy
  • 1,949
  • 5
  • 20
  • 22
  • you need to add jTextField.getDocument().addDocumentListener(new DocumentListener(){ ... }). – Liu guanghua Jul 05 '12 at 02:36
  • @Liuguanghua hi I have tried just that. However it fires an event even when I am inputting text into it. How can I make it such that the event is fired after the person has finished inputting the text into the jtextfield?(regardless of whether its empty field or not) – Laughy Jul 05 '12 at 02:45

2 Answers2

3

One approach is to leverage the value property in JFormattedTextField, as shown in this example and outlined here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

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();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • @Laughy : If you leave the `JTextField` blank, then you are bound to get `NumberFormatException`, since you won't be able to convert `""` to an Integer Value. – nIcE cOw Jul 05 '12 at 07:10