清除当前的FocusOwner(jTextfield) [英] Clear current FocusOwner (jTextfield)

查看:110
本文介绍了清除当前的FocusOwner(jTextfield)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


快速开发应用程序,只需查询一下:-

我想使用一个按钮清除当前焦点所有者文本字段.可以使用 isFocusOwner()来确定文本字段是否是当前焦点所有者,但是可以使用如何清除当前焦点所在的文本字段?


Developing an application in swing, just a little query :-

I want to clear the current focus owner textfield using a button. It is possible to determine whether a textfield is the current focus owner or not using isFocusOwner() but how to clear the textfield which is currently on focus?

谢谢!

推荐答案

您也许可以使用TextAction. TextAction可以访问具有焦点的最后一个文本组件.因此,在文本操作中,您只需清除组件中的文本即可.所有逻辑都完全包含在一个地方.

You might be able use a TextAction. A TextAction has access to the last text component that had focus. So then in the text action you just clear the text in the component. All the logic is fully contained in the one place.

这里是一个示例,演示了使用TextAction的概念.在这种情况下,按钮所代表的数字将附加到具有焦点的文本字段中:

Here is an example that demonstrates the concept of using a TextAction. In this case the number represented by the button is appended to the text field with focus:

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

public class NumpadPanel extends JPanel
{
    public NumpadPanel()
    {
        setLayout( new BorderLayout() );

        JTextField textField1 = new JTextField(4);
        JTextField textField2 = new JTextField(2);
        JTextField textField3 = new JTextField(2);

        JPanel panel = new JPanel();
        panel.add( textField1 );
        panel.add( textField2 );
        panel.add( textField3 );
        add(panel, BorderLayout.PAGE_START);

        Action numberAction = new TextAction("")
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JTextComponent textComponent = getFocusedComponent();

                if (textComponent != null)
                    textComponent.replaceSelection(e.getActionCommand());
            }
        };

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setMargin( new Insets(20, 20, 20, 20) );
            button.setFocusable( false );
            buttonPanel.add( button );
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Numpad Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new NumpadPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

在这种情况下,您将只使用setText()方法,而不是使用replaceSelection()方法.

In your case instead of using the replaceSelection() method you would just use the setText() method.

这篇关于清除当前的FocusOwner(jTextfield)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆