user3802348
user3802348

Reputation: 2602

Setting JTextField visible on JRadioButton Click

I'm creating a GUI in Netbeans and I want to set a text field to appear when a radio button is selected. For some reason, the radio button click is detected, but the text field does not appear upon selection. Any suggestions on how to handle this problem? My code is pasted below. The text field is called newContainerNameInput, and the radio button is newContainerRadioButton:

    containersButtonGroup.add(newContainerRadioButton);
    newContainerRadioButton.setText("Create a new container");
    newContainerRadioButton.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            newContainerRadioButtonItemStateChanged(evt);
        }
    });
    newContainerRadioButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            newContainerRadioButtonActionPerformed(evt);
        }
    });

    newContainerNameInput.setText("Enter new container name here");
    newContainerNameInput.setVisible(false);

private void newContainerRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                        
    System.out.println("test");
    newContainerNameInput.setVisible(true);
}  

Upvotes: 1

Views: 1185

Answers (1)

Ramesh-X
Ramesh-X

Reputation: 5085

Replace following code in the ActionListener of RadioButton

private void newContainerRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                        
  System.out.println("test");
  newContainerNameInput.setVisible(true);
  revalidate();
} 

revalidate() is doing 2 things. First invalidate() and validate(). By doing this your components get marked invalid and validated again. That means layout again.. For more see javadoc

Upvotes: 1

Related Questions