Problems with the JCheckBox item listener

0

I am using this code to make the key entered in the JPasswordfield "jp_clave" readable by checking the JCheckBox "jcb_key"

private void jcb_claveActionPerformed(java.awt.event.ActionEvent evt) {                                          

    jcb_clave.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            jp_clave.setEchoChar((char) 0);  

        } else {
            jp_clave.setEchoChar('*');

        }
    }
});

The problem is that when running the program I have to check and check the JCheckBox for the first time to work as it should, maybe it is some silly mistake so I ask you to be patient and help me please.

EDITED

Apparently I was giving a lot of laps to the code and thanks to TwoDent my dilemma is solved, regarding the JPasswordField code:

jp_clave = new javax.swing.JPasswordField();    

jp_clave.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jp_clave.setText("");

It's all I could track since the code was automatically generated when I dragged the component into the form, by default any text that is written in the pass becomes as if a password was entered and the code in the checkbox (if it was checked) forced to show the text as you typed it

    
asked by J.Calderon 05.02.2017 в 05:54
source

1 answer

0

Why do not you try this:

if(MiCheckBox.isSelected()){
    //mis acciones
}

In other words, in your case:

if(jcb_clave.isSelected()){
   jp_clave.setEchoChar((char)0);
}

The isSelected() function checks if your checkbox is activated or not.

    
answered by 05.02.2017 / 06:01
source