简体   繁体   English

在循环内创建具有不同名称的对象(JTextfields)

[英]Creating objects (JTextfields) inside loop with different names

Is it possible to create multiple JTextFields with different names inside a loop?是否可以在循环内创建多个具有不同名称的JTextFields
I need to so this since I later need to get the text that is in each of them, I need to do something like this:我需要这样做,因为我以后需要获取每个文本中的文本,我需要执行以下操作:

while (sst_ResultSet.next()) {
    p_vertabla.add(new JLabel(sst_ResultSet.getString(1));
    p_vertabla.add(new JTextField(sst_ResultSet.getString(1)));
}

This works just fine when adding the JLabel and the JTextField the way I want it to the JPanel p_vertabla , but I don't know how to later call the method .getText();当将JLabelJTextField按照我想要的方式添加到JPanel p_vertabla ,这工作得很好,但我不知道以后如何调用方法.getText(); on the JTextFields .JTextFields


How can I create the JTextFields in the loop or how can I later call the method on them?我如何在循环中创建JTextFields或者我以后如何调用它们的方法?

You can create a Vector<JTextField> and add to it the generated items in your for loop:您可以创建一个Vector<JTextField>并将生成的项目添加到您的 for 循环中:

while (sst_ResultSet.next()) {
    p_vertabla.add(new JLabel(sst_ResultSet.getString(1));
    createAndKeepJTextFieldInVector(sst_ResultSet.getString(1));
    // instead of original: p_vertabla.add(new JTextField(sst_ResultSet.getString(1)));
}

Then later, you could access any of the JTextFields by calling:然后,您可以通过调用访问任何 JTextFields:

String txt = vector.get(index).getText();

I'm answering my own question with a little from a comment on the question, I found out that there is no need to assign the variables a name, with an ArrayList<JtextFiel> will work, the point is just to follow the order in what they were added to the JPanel that way I was able to retrieve the information associated with the JTextField in that particular position.我正在通过对该问题的评论来回答我自己的问题,我发现没有必要为变量分配一个名称,使用ArrayList<JtextFiel>就可以了,关键是要遵循中的顺序它们被添加到JPanel方式我能够检索与该特定位置的JTextField关联的信息。 I did something like this:我做了这样的事情:

ArrayList<JLabel> listadelabels = new ArrayList<JLabel>();
ArrayList<JTextField> listadetextfields = new ArrayList<JTextField>();
ArrayList<JCheckBox> listadecheckbox = new ArrayList<JCheckBox>();
...
Statement stmt=db.createStatement();
    ResultSet sst_ResultSet = stmt.executeQuery(query);
    JPanel p_obtenerregistro = new JPanel(new GridLayout(0,3));
    while (sst_ResultSet.next()) {
        listadelabels.add(new JLabel(sst_ResultSet.getString(1)));
        listadetextfields.add(new JTextField(12));
        listadecheckbox.add(new JCheckBox());
    }
    for(int i=0 ; i<listadelabels.size(); i++){
        listadecheckbox.get(i).addItemListener(this);
        p_obtenerregistro.add(listadecheckbox.get(i));
        p_obtenerregistro.add(listadelabels.get(i));
        listadetextfields.get(i).setEditable(false);
        p_obtenerregistro.add(listadetextfields.get(i));
    }

And then to get the info associated with the list I did it like this:然后为了获取与列表相关的信息,我是这样做的:

for (int i = 0; i < activos; i++) {
            atributos[i] = listadetextfields.get(arreglodeactivos[i]).getText();
            columnas[i] = listadelabels.get(arreglodeactivos[i]).getText();
            ModificarRegistro.where = ModificarRegistro.where + columnas[i] + "=" + "'" + atributos[i] + "' AND ";
        }

I really hope it could useful for a lot of people我真的希望它对很多人有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在循环中创建具有不同名称的多个对象以存储在数组列表中 - Creating multiple objects with different names in a loop to store in an array list 在 for 循环中创建 JTextFields 以逐步计算数字的 pow - Creating JTextFields in a for loop to calculate pow of a number stepwise 在Java的while循环中为对象赋予不同的名称 - Giving objects different names in a while loop in Java Java:在运行时创建具有不同名称的数组对象并访问/更新它们 - Java: creating objects of arrays with different names at runtime and accessing/updating them 为JTextField设置不同的inputVerifier - Set different inputVerifier for JTextFields 在Loop内部创建多个Java对象并在外部使用所有对象 - Creating multiple Java objects inside Loop and using all outside 在循环内创建对象与在循环前创建一个临时对象 - Creating objects inside loop vs. creating one temp object before loop 在 for 循环中实例化数组以创建 JTextFields - Instantiating array in a for loop to create JTextFields 如何处理列表中的对象,我能否在循环中创建多个具有不同名称的对象? - How do I work with objects in lists and can I create multiple objects with different names in a loop? 使用for循环创建对象 - Creating objects with a for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM