1

I have a jframe with a jcheckbox and a jtextfield (it has many more components).

I set the label to setVisible(false) and when the checkbox is checked, it should turn the label visible. It actually does, but you just can´t see it until you click anywhere else on the frame.

Here is some of the code:

jTextField17 = new javax.swing.JTextField();
jTextField17.setText("Quantas?");
jTextField17.setVisible(false);
jTextField17.setMinimumSize(new java.awt.Dimension(52, 20));


jCheckBox1 = new javax.swing.JCheckBox();
jCheckBox1.setBackground(new java.awt.Color(153, 255, 153));
jCheckBox1.setText("Cabecinhas");
jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
   public void actionPerformed(java.awt.event.ActionEvent evt) {
    jCheckBox1ActionPerformed(evt);
   }
});


private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (jCheckBox1.isSelected()){
        jTextField17.setVisible(true);
    }else{
        jTextField17.setVisible(false);
    }
}                                       

I don´t think there´s anything wrong with the code.

To sum up:

user clicks checkbox. checkbox gets marked, nothing else seems to happen. user clicks anywhere on screen (after checking checkbox) and the textfield appears.

I´m using netbeans GUI editor. I would like to show the whole code, but it´s 3000+ lines. If you need to see more, ask me what part you need and I´ll edit here. Thanks for reading this and thank you even more for trying to help.

DrNetwork
  • 11
  • 3
  • I now believe the problem is on the code generated by netbeans. I made another frame from scratch without GUI and it works as it should. Has anyone ever come across this problem on netbeans IDE? – DrNetwork Aug 11 '15 at 16:54
  • I use netbeans to, it's not netbeans it's your code. I placed an answer down below of adding one line. Try it. – kayleighsdaddy Aug 11 '15 at 16:56
  • http://stackoverflow.com/questions/9882845/jcheckbox-actionlistener-and-itemlistener Nice link describes the difference – Shriram Aug 11 '15 at 16:57

3 Answers3

4

Try using this.repaint(); and this.revalidate();

private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (jCheckBox1.isSelected()){
        jTextField17.setVisible(true);
        this.repaint();
        this.revalidate();

    }else{
        jTextField17.setVisible(false);
    }
}          
Chamith Chathuka
  • 605
  • 1
  • 6
  • 18
  • Worked like a charm! Do you know why this happened? Although many people are just looking for a piece of code, I´m trying to understand java. – DrNetwork Aug 11 '15 at 18:27
  • If it worked could you please accept my answer ? so others also could benefit of that! – Chamith Chathuka Aug 11 '15 at 18:33
  • @DrNetwork repaint() method causes a call to this component's paint method as soon as possible Otherwise, this method causes a call to this component's update method as soon as possible. update(Graphics g) method of Component calls this component's paint method to redraw this component. Its like refreshing the screen..as I think! – Chamith Chathuka Aug 11 '15 at 18:41
  • @DrNetwork for further reference use http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html – Chamith Chathuka Aug 11 '15 at 18:44
0

You need to repaint it make it, otherwise it waits until it is repainted.

private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (jCheckBox1.isSelected()){
        jTextField17.setVisible(true);
        jTextField17.repaint();
    }else{
        jTextField17.setVisible(false);
    }
}    
kayleighsdaddy
  • 670
  • 5
  • 15
0

If you will not initially set the textfield's setVisible method to false, you may do away with this issue.

d_air
  • 631
  • 1
  • 4
  • 12