mercredi 3 mai 2017

Validate JOptionPane text fields

Please help me validate (make sure) all four of my JOptionPane text fields are filled with some information before adding information to the tableModel. If some of the fields are empty, warning dialogue must be shown. And JOptionPane must be reopened for user to complete all of four fields.

    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);
        }
    }
});

Aucun commentaire:

Enregistrer un commentaire