[英]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();
当将JLabel
和JTextField
按照我想要的方式添加到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.