当我删除Jtextfield中的文本时,lostFocusEvent没有出现?

时间:2012-07-05 01:58:44

标签: java swing jtextfield jformattedtextfield

您好我已经实现了一个简单的focusListener方法并向其注册了两个jTextFields。此方法的作用是在其中添加数字并将其显示在JLabel中。如果我在其中键入“2”,它将正确更新以给我4.但是,如果我之后只删除2,即使我点击其他地方也不会被触发focusLost事件。如果我在JTextField中输入0,则focusLost事件将正常发生。为什么会这样?谢谢!

2 个答案:

答案 0 :(得分:3)

一种方法是利用JFormattedTextField中的value属性,如example所示,并概述here

答案 1 :(得分:3)

如果没有THE SSCCE,很难说出你所使用的逻辑。因为在下面的示例中,它正如您所期望的那样工作:

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();
            }
        });
    }
}