CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2004
    Location
    Texas
    Posts
    24

    checkboxes and netbeans

    Hi! It's been awhile since I've been here. I'm moving on to using java as well as visual basic and I'm having MANY troubles!!

    Code:
      
    private void chkShowActionPerformed(java.awt.event.ActionEvent evt) {                                        
            lblShowText.setVisible(false);   
    }
    When I run this I can uncheck the box and the label will disappear. This is good... however; I want it to come back when the checkbox IS checked. I'm used to a little "if" statement and I can't figure out how to ask: "If it is checked, show label, if not, don't show label"

    Thanks!!!

    PS: I'm using Ivor Horton's Java book as a reference, but it's not as helpful with Netbeans... any other reference text suggestions?
    GO TECH GO!!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: checkboxes and netbeans

    You haven't said what GUI component you are using so I am assuming it is a JCheckBox.

    The ActionEvent object contains a reference to the object that generated the event - in this case your JCheckBox. You can get the component by calling getSource(). So now you know which component generated the event you can find out if it is selected and set the label accordingly. JCheckBox has an isSelected() method so you could use something like
    Code:
    JCheckBox myComponent = (JCheckBox)evt.getSource();
    lblShowText.setVisible(myComponent.isSelected());
    Note: this assumes only JCheckBox components will generate events that are passed to this method.

  3. #3
    Join Date
    Sep 2004
    Location
    Texas
    Posts
    24

    Re: checkboxes and netbeans

    I'm sorry about not being clearer about what components I was using...

    Yes, it was a checkbox and this code fixed it!! Thank you so much, keang!!!


    For anyone else looking at this thread: Since I am using Netbeans GUI creator, I did not have to declare the JCheckBox component so it was one line of code I had to rewrite.
    GO TECH GO!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured