I have created a form in netbeans with two radio buttons in a button group with a text box. Box 1 = Yes Box 2 = No when form loads no is already selected. I want the text box to be ivisible until user selects yes. but everything I have tried is not working. I put this in the form init part txtMajorAmt.setVisible(false); did not work. But after everything is loaded if I click on yes then no it starts working correct. any help would be appreciated.

Thank you.

everything I have tried is not working.

Could you make a small, simple complete program that compiles, executes and shows the problem?

Edited 4 Years Ago by NormR1

ok this is almost like the code in my program:

package javaapp;

/**
 *
 * @author J. R. Gillispie
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
        txtMajorAmt.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        btnYes = new javax.swing.JRadioButton();
        btnNo = new javax.swing.JRadioButton();
        txtMajorAmt = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        buttonGroup1.add(btnYes);
        btnYes.setText("Yes");
        btnYes.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                btnYesItemStateChanged(evt);
            }
        });
        btnYes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnYesActionPerformed(evt);
            }
        });

        buttonGroup1.add(btnNo);
        btnNo.setSelected(true);
        btnNo.setText("No");
        btnNo.setActionCommand("No");
        btnNo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                btnNoItemStateChanged(evt);
            }
        });
        btnNo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNoActionPerformed(evt);
            }
        });

        txtMajorAmt.setText("  ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnYes)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnNo)
                        .addGap(103, 103, 103)
                        .addComponent(txtMajorAmt, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(160, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(btnYes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtMajorAmt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNo))
                .addContainerGap(180, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void btnYesActionPerformed(java.awt.event.ActionEvent evt) {
       txtMajorAmt.setVisible(true);
    }

    private void btnNoActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        txtMajorAmt.setVisible(false);
    }

    private void btnNoItemStateChanged(java.awt.event.ItemEvent evt) {
        // TODO add your handling code here:
        txtMajorAmt.setVisible(false);
    }

    private void btnYesItemStateChanged(java.awt.event.ItemEvent evt) {
        // TODO add your handling code here:
        txtMajorAmt.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JRadioButton btnNo;
    private javax.swing.JRadioButton btnYes;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField txtMajorAmt;
    // End of variables declaration
}

There may be a problem with your ItemStateChanged methods. They will be called whenever the state changes - ie they become selected or deselected, but your code is intended to run only when the state becomes selected (?)

Only if I remove the line in Public new jframe()
txtMajorAmt.setVisible(false);
then it will work if i click on yes then no. but i want it to be invisible when it is loaded.

Try calling the textfield's container class's validate method so it knows there has been a change to one of its components.

Edited 4 Years Ago by NormR1

I just re-read this thread, and nowhere do you state exactly what is wrong with the behaviour. Can you please explain, one step at a time, what the visibility of the text field should be and what it actually is at each step?

This article has been dead for over six months. Start a new discussion instead.