aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JCheckBox and event Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JCheckBox and event Handling" Watch "JCheckBox and event Handling" New topic
Author

JCheckBox and event Handling

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
There is a requirement that if the checkbox is selected a label would be added to the panel,and if again, its de-selected, it should be removed or disappear.

Here is what i tried


This adds the 'nameLabel' at selection, but again if i de-select it, it still appears in the panel, here is what i tried BUT FAILED :


What should i do?


kunal
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
It seems that if i de-select it, it doesn't goes for block
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Solved to some extend

earlier in the panel i added label and textfield and made both of them not visible by coding as




Later on, in case of selection/ deselection



But it works fine if i just make a single component visible and unvisible (like only name label)
The above code gives following exception with only label added in the panel


What is wrong with the code?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

You could have prevented your earlier troubles if you immediately had used the constants. The 1 was not the problem, that's the value of ItemEvent.SELECTED. Your fault was that you assumed ItemEvent.DESELECTED is 0, but it is not - it's 2. By using ItemEvent.DESELECTED from the start you probably would have encountered this new error sooner.

That error is easy to troubleshoot. Something on line 358 of class org.nit.StudentDatabase2 is null, inside the itemEvent method. If your last post contains the up-to-date code that would be either name or nametext. That would mean that you are trying to access them before they are initialized, possibly by adding the listener and then changing the state all while still initially constructing the user interface.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks for your reply Rob.

What i did was initialized it twice. (Once completely declared and initialized inside a method, and and then declared at class level, but initialized again)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JCheckBox and event Handling