I would like to validate with each change the size of the text, perform an action! I put one while isolated, 2 ifs inside an ActionListener, created a separate method and it didn't work. The idea is: while the person types in JTextfield, if the text size is different from 11, a Jlabel below the textfield will turn red and with an invalid message; if it is equal to 11, it will turn green and the message will be valid. And this validation must be done as the user types. (I wouldn't want to put a button to validate when clicked).
I solve this with DocumentListener:
package Janelas;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import javax.swing.event.DocumentListener;
import javax.swing.SwingConstants;import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Cadastro extends JPanel {
private JTextField textFieldCampoNIS;
private JLabel lblStatus = new JLabel("");
public Cadastro(String titulo) {
setBackground(new Color(0, 0, 0));
setLayout(null);
JLabel lblNewLabel = new JLabel(titulo);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 24));
lblNewLabel.setForeground(new Color(222, 221, 218));
lblNewLabel.setBounds(12, 46, 426, 35);
add(lblNewLabel);
JLabel lblNISFamilia = new JLabel("NIS da Família:");
lblNISFamilia.setHorizontalAlignment(SwingConstants.CENTER);
lblNISFamilia.setForeground(new Color(222, 221, 218));
lblNISFamilia.setFont(new Font("Dialog", Font.BOLD, 18));
lblNISFamilia.setBounds(12, 110, 158, 35);
add(lblNISFamilia);
JLabel lblStatus = new JLabel("");
lblStatus.setBounds(165, 152, 158, 17);
add(lblStatus);
textFieldCampoNIS = new JTextField();
textFieldCampoNIS.setBounds(165, 119, 158, 21);
add(textFieldCampoNIS);
textFieldCampoNIS.setColumns(11);
textFieldCampoNIS.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
int sizer =textFieldCampoNIS.getText().length();
//System.out.println(sizer);
if (sizer != 11 && sizer != 0) {
lblStatus.setText("NIS inválido!");
lblStatus.setForeground(new Color(200, 10, 10));
} else if (sizer == 11) {
lblStatus.setText("NIS é válido!");
lblStatus.setForeground(new Color(10, 200, 10));
} else {
lblStatus.setText("");
}
}
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
int sizer =textFieldCampoNIS.getText().length();
//System.out.println(sizer);
if (sizer != 11 && sizer != 0) {
lblStatus.setText("NIS inválido!");
lblStatus.setForeground(new Color(200, 10, 10));
} else if (sizer == 11) {
lblStatus.setText("NIS é válido!");
lblStatus.setForeground(new Color(10, 200, 10));
} else {
lblStatus.setText("");
}
}
@Override
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
});
}
}