0

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

dorzao
  • 1
  • 1
  • What changes the size of the text? You should not be polling with a while loop, but in stead should be listening for changes in size at the source of where the size change occurs. This code us unknown to us. Please create and post a [mre] (please read the link) to help us to fully understand your code and your problem. – Hovercraft Full Of Eels Jul 23 '23 at 02:24
  • 1
    @HovercraftFullOfEels I think they mean length, i.e. a character being typed. – tgdavies Jul 23 '23 at 02:26
  • @tgdavies, ah, that would make sense. Then I would use a DocumentListener, not a KeyListener and *never* a loop. – Hovercraft Full Of Eels Jul 23 '23 at 02:27
  • @tgdavies, which means that your dupe is a valid dupe. – Hovercraft Full Of Eels Jul 23 '23 at 02:27
  • thanks everyone! i was solved whith DocumentListener using @override insertUpdate and removeUpdate methods! – dorzao Jul 23 '23 at 17:49

0 Answers0