2015-01-14 124 views
-1

清除所有JTextField的我需要staticmethod我可以保持在任何publicclass,其中我将传递一个JInternalFrame的对象,它会自动清除所有的TextField特定JInternalFrame(使用环)..通过功能

我已经通过很多的答案了,但他们简单地使用setText("")单独为所有的TextField的状态,但那不是生产性的,仅仅是因为在我的项目我计划50个表格和超过500米的TextField ..

它如果有人向我提供带有通讯的片段,将会有所帮助因此我可以理解其他组件的工作和实现,如复选框,收音机等我自己的

+0

你如何填充这些文本框的数据?那些与一些模型对象绑定?如果是,那么你可以简单地重置模型对象。 – eatSleepCode

+0

否它们没有绑定,textFields即文本框是用户输入的普通字段。 –

回答

2

保持简短。

类具有静态函数:

public class JCTest 
{ 
    public static void Clear(JInternalFrame intFrame) 
    { 
     if (intFrame == null) 
      return; 
     Container con = intFrame.getContentPane(); 
     for (Component c : con.getComponents()) 
     { 
      if (c instanceof JTextField) 
      { 
       JTextField j = (JTextField)c; 
       j.setText(""); 
      } 
     } 
    } 
} 

称之为:

public class Main 
{ 
    public static void main(String[] args) 
    { 
     JInternalFrame intFrame = new JInternalFrame(); 
     JCTest.Clear(intFrame); 
    } 
} 
+0

卓越的代码,工程非常好,简短和简单:) –

+0

可以编辑它吗?我的意思是你可以修改它,以便我可以把它作为静态方法放入类中,并将JinternalFrame的对象传递给它并清除所有内容?代码是好的,我把它放在相同的框架..你可以修改它? –

+0

@RajitBansal修改它。这不是那么难... – Blacktempel

1

基本上,您可以调用getComponents()方法,该方法将返回所有子组件。然后你必须检查它是否是JTextField类型,然后你可以调用.setText(“”)。如果您想为所有Swing组件提供类似的解决方案,则可以使用Document,这是组件显示的数据的抽象。这里是一个例子:

if (rootComponent instanceof Container) { 
    Component[] subComponents = ((Container) rootComponent).getComponents(); 
     for (Component c : subComponents) { 
      if (c instanceof JTextField) 
       c.setText(""); 
     } 
    } 

编辑这是从我的项目,我已经分裂成两种方法。 ApplyColors,因为我使用它来着色组件。我认为它也适用于setText()。

public static void applyColors(Window parent) { 
    List<String> colorComponentsClassNames = Arrays.asList("JTextField"); 
     for (Component component : getAllComponents(parent)) { 
     Component[] components = ((Container) component).getComponents(); 
     for (int i = 0; i < components.length; i++) { 
      Component currentComponent = components[i]; 
      if (!colorComponentsClassNames.contains(currentComponent.getClass().getSimpleName())) { 
       continue; 
      } 
      currentComponent.setText("")); 

     } 
    } 
} 

public static List<Component> getAllComponents(final Container c) { 
    Component[] comps = c.getComponents(); 
    List<Component> compList = new ArrayList<Component>(); 
    for (Component comp : comps) { 
     compList.add(comp); 
     if (comp instanceof Container) { 
      compList.addAll(getAllComponents((Container) comp)); 
     } 
    } 
    return compList; 
} 
+0

这将只适用于一个'容器'... – MadProgrammer

+0

如果我想在其他类中使用它,我可以使用它作为全局函数,我可以将Form或rootCompnent作为参数传递给它吗? –

+0

那就是@MadProgrammer的问题。只需传入JInternalFrame即可。 –

1

你可以...

使用Container#getComponents列出一个给定的Container的所有子组件,您将需要检查每个Component和测试,看它是否是一个instanceofJTextField,当你发现一个组件,它将其投射并使用setText来清除该字段。您还需要测试每个Component,看它是否是一个instanceofContainer和执行递归搜索,如getComponents将只返回指定Container

眼前的孩子这是怎么样的混乱和费时的,因为你需要遍历整个组件层次结构以确保找到所有字段。这也意味着,它会清除你其实并不想清楚领域...

你可以...

创建一个自定义类,从JInternalFrame或其他一些类像JPanel,其中有一个延伸方法,例如clearFields(例如),它可以简单地循环通过JTextFieldList。然后

您需要添加要通过这种方法管理的List各个领域,但它是一个简单的机制......

public class MyAwesomeForm extends JPanel { 
    private List<JTextField> fields; 

    public void registerField(JTextField field) { 
     fields.add(field); 
    } 

    public void unregisterField(JTextField field) { 
     fields.remove(field); 
    } 

    public void clearFields() { 
     for (JTextField field : fields) { 
      field.setText(null); 
     } 
    } 
} 

您所有的形式都需要从这个当延长您只需拨打clearFields给定的表格...

+0

基本上对于我使用NetBeans的项目,它不会自动添加所有组件到列表中,并且手动执行它会消耗很多时间..没有使用LIST的任何其他代码 –

+0

嗯,我认为第一种方法更好......因为为什么我们应该浪费内存(使用第二种方法)注册/存储'jtextfield'(s)如果容器确实为我们维护它!而第一种方法并不是凌乱的...... – Junaid

+0

@Junaid因为它像使用大锤杀死苍蝇一样,它可能有效,但是在这个过程中你往往会破坏更多的东西。第一个建议也不允许您“过滤”该过程,以避免重置您可能不想清除的字段。它也使用递归方法,填满堆栈,所以如果你担心内存,第二种方法实际上更便宜和更快。 – MadProgrammer