Note that there are some explanatory texts on larger screens.

plurals
  1. POMake a textfield visible on itemStatechanged event of a checkbox
    primarykey
    data
    text
    <p>How to make a text field visible on <code>itemStatechanged</code> event of a check box in Swing?</p> <p>I am trying to create a frame with a check box and a text field. I want the text field to be displayed only when the check box is selected. So when I initialize the components, I have set the <code>textfield.setvisible</code> to <code>false</code> and for the check box added a <code>addItemListener</code> and call the <code>itemStateChanged</code> event and there is the check box is selected, I set the <code>setVisible</code> method to <code>true</code>. </p> <p>My SSCCE looks like:</p> <pre><code>package ui; public class Evaluator extends javax.swing.JFrame { public Evaluator() { initComponents(); } private void initComponents() { jCheckBox1 = new javax.swing.JCheckBox(); jTextField1 = new javax.swing.JTextField(); jTextField1.setVisible(false); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setPreferredSize(new java.awt.Dimension(800, 800)); jCheckBox1.setFont(new java.awt.Font("Tahoma", 0, 14)); jCheckBox1.setText("Properties"); jCheckBox1.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { jCheckBox1ItemStateChanged(evt); } }); jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jCheckBox1) .addGap(41, 41, 41) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(155, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(229, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jCheckBox1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(34, 34, 34)) ); pack(); } private void jCheckBox1ItemStateChanged(java.awt.event.ItemEvent evt) { // TODO add your handling code here: if(evt.getStateChange()== java.awt.event.ItemEvent.SELECTED){ jTextField1.setVisible(true); } } public static void main(String args[]) { 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(Evaluator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Evaluator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Evaluator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Evaluator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Evaluator().setVisible(true); } }); } private javax.swing.JCheckBox jCheckBox1; private javax.swing.JTextField jTextField1; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload