请在向tableModel添加信息之前,帮助我验证(确保)所有四个JOptionPane文本字段都填充了一些信息。 如果某些字段为空,则必须显示警告对话框。必须重新打开JOptionPane才能让用户完成所有四个字段。
newUser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String[] options = {"Ierakstīt", "Atcelt"};
ImageIcon icon = new ImageIcon("C:\\Users\\AdrianP\\Desktop\\kursovaja\\User-48.png");
int n = JOptionPane.showOptionDialog(w, myPanel, "Pievienot jaunu lietotāju", JOptionPane.PLAIN_MESSAGE, 0, icon, options, options[1]);
if (n == 0) {
if (nameField.getText().equals("") && lastNameField.getText().equals("") && groupField.getText().equals("") && idField.getText().equals("")) {
JOptionPane.showMessageDialog(w, "Kļūda!", "Lūdzu, ievadiet datus!", 0, icon);
nameField.setText(null);
lastNameField.setText(null);
idField.setText(null);
groupField.setText(null);
JOptionPane.showOptionDialog(w, myPanel, "Pievienot jaunu lietotāju", JOptionPane.PLAIN_MESSAGE, 0, icon, options, options[1]);
} else {
tableModel.addRow(new Object[] {nameField.getText(), lastNameField.getText(), idField.getText(), groupField.getText()});
nameField.setText(null);
lastNameField.setText(null);
idField.setText(null);
groupField.setText(null);
}
} else {
nameField.setText(null);
lastNameField.setText(null);
idField.setText(null);
groupField.setText(null);
}
}
});
答案 0 :(得分:0)
在您的情况下,根据需要检查所有部件:
if (nameField.getText().equals("") && lastNameField.getText().equals("") && groupField.getText().equals("") && idField.getText().equals(""))
如果您想检查是否有任何TextFields是空的而且没有看到其他TextFields,您可以更改“&&”到“|”或“||”。例如:
if (nameField.getText().equals("") | lastNameField.getText().equals("") | groupField.getText().equals("") | idField.getText().equals(""))
您可以使用更复杂的构造来组织您的条件,例如,使用JOptionPane:
int rc = JOptionPane.showOptionDialog(null, "The typeR : is not loaded. Do you want to load or continue?", JOptionPane.WARNING_MESSAGE, 0, null, buttons, buttons[2]);
if (rc == 0) {
for (int i = 1; i <= colCountState; i++) {
// to do something
}
if (rc == 1) {
for (int i = 1; i <= colCountState; i++) {
// to do something
}
}