Search code examples
javaswingjtextfieldjradiobuttonchangelistener

How to hide a Text Field when a radio button is selected/not selected?


I'm writting a program to encript data. It has a JTextArea to edit text, but I want to choose if I save it encripted or in plain text. For this I created a JDialog that appears when the save button is clicked. It contains two radio buttons: one to save the data encripted and the other to save in plain text. In the middle of them there is a JPasswordField requesting the key of the encription.

My question is if there is a simple way of making the TextField not useable and half transparent, when the option to save encripted is not selected. Or, if there isn't a simple way of doing it, a way to hide the TextArea. I tryed using a ChangeListener on the radio button but it isn't working. Here is my code:

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class StackOverflowVersion extends JFrame {

public static JFrame frame;

public StackOverflowVersion() {
    dialogoCriptografar();
    System.exit(EXIT_ON_CLOSE);
}

public void dialogoCriptografar(){
    final ButtonGroup bGroup = new ButtonGroup();
    JRadioButton[] buttons = new JRadioButton[2];
    final JPasswordField passwordField = new JPasswordField(20);

    // create the raio bunttons
    buttons[0] = new JRadioButton("Encript document before saving");
    buttons[1] = new JRadioButton("Just save it");
    //ad them to the ButtonGroup
    bGroup.add(buttons[0]);
    bGroup.add(buttons[1]);
    // select the option to encript
    buttons[0].setSelected(true);

    //creates a panel with the radio buttons and a JPasswordField
    final JPanel box = new JPanel();
    JLabel descricao = new JLabel("Choose an option to save:");
    box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
    box.add(descricao);
    box.add(buttons[0]);
    box.add(passwordField);
    box.add(buttons[1]);

    // creates the dialog
    final JDialog dialogo = new JDialog(frame, "Storage options", true);
    dialogo.setSize(275,125);
    dialogo.setLocationRelativeTo(frame);
    dialogo.setResizable(false);
    dialogo.add(box);
    dialogo.setVisible(true);

    /* Doesn't work:
    buttons[0].addChangeListener(new ChangeListener(){
        boolean visivel = true;//ele começa visivel
        public void stateChanged(ChangeEvent event){
            if(visivel){
                box.remove(password);
                SwingUtilities.updateComponentTreeUI(dialogo);
                dialogo.revalidate();
                dialogo.repaint();
                visivel = false;
            }
            else{
                box.add(password);
                SwingUtilities.updateComponentTreeUI(dialogo);
                dialogo.revalidate();
                dialogo.repaint();
                SwingUtilities.updateComponentTreeUI(dialogo);
                visivel = true;
            }
        }
    });
    */
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
           frame = new StackOverflowVersion();
           frame.setVisible(true);
        }
    });
}
}

Solution

  • My question is if there is a simple way of making the TextField not useable and half transparent,

    Have you tried a simple

    textField.setEditable(false);
    

    Or

    textField.setEnabled(false);