为什么在此事件之后执行JFormattedTextField
对象不更新文本??
为什么在setText("")
调用文本后清楚但在jComboBox1ItemStateChanged
再次使用另一个状态后,最后一个文本又回来了
public class AddItem extends JDialog {
public ArrayList< JFormattedTextField> jtxts = new ArrayList<>();
public AddItem(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
for (int i = 0; i < 3; i++) {
jtxts.add(new JFormattedTextField());
jtxts.get(i).setVisible(false);
jtxts.get(i).setBounds(450, 80 + i * 40, 100, 20);
add(jtxts.get(i));
jlabels.add(new JLabel());
jlabels.get(i).setBounds(560, 80 + i * 40, 100, 20);
jlabels.get(i).setVisible(false);
jlabels.get(i).setLabelFor(jtxts.get(i));
add(jlabels.get(i));
}
}
private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
switch (jComboBox1.getSelectedIndex()) {
case 0:
hideJtxtAndJlabels();
showJtxtAndJlabels(3);
break;
case 1:
hideJtxtAndJlabels();
showJtxtAndJlabels(2);
break;
}
}
public void showJtxtAndJlabels(int size) {
for (int i = 0; i < size; i++) {
jtxts.get(i).setVisible(true);
jlabels.get(i).setVisible(true);
}
}
private void hideJtxtAndJlabels() {
for (int i = 0; i < jlabels.size(); i++) {
System.out.println(jtxts.get(i).getText()); //print the last text
jtxts.get(i).setText("");
System.out.println(jtxts.get(i).getText()); //print "" but after change state jcombobox1 the last text is back
jlabels.get(i).setVisible(false);
jtxts.get(i).setVisible(false);
}
}
}