|
-
April 19th, 2007, 03:59 PM
#1
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?
-
April 19th, 2007, 06:24 PM
#2
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.
-
April 20th, 2007, 08:21 AM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|