如何清除JPanel中的所有输入字段 [英] How to clear all input fields within a JPanel

查看:54
本文介绍了如何清除JPanel中的所有输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPanel 中提交记录后,有没有办法清除所有输入字段(JTextFieldJComnboBox 等)?

Is there a way to clear all input fields (JTextField, JComnboBox, etc) after record submission within a JPanel ?

目前我所做的是访问每个组件并单独使用setText("")

Currently what I do is, to access to each component and individually use the setText(""), etc.

推荐答案

也许是这样的:

for(JComponent control : parentPanel.getComponents())
{
    if(control instanceof JTextField)
    {
        JTextField ctrl = (JTextField) control;
        ctrl.setText("");
    }
    else if (control instanceof JComboBox)
    {
        JComboBox ctr = (JComboBox) control;
        ctrl.setSelectedIndex(0);
    }
}

这应该遍历 JPanel 中的每个组件并检查该组件是 JTextField 还是 JComboBox 并相应地重置.

This should iterate over each component within the JPanel and check if the component is a JTextField or a JComboBox and reset accordingly.

这篇关于如何清除JPanel中的所有输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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