Skip to content
Advertisement

checkbox appears when button pressed, java

My code displays a texture where you can type something in and than it should appear under the label with a checkbox. What I want is that the checkbox is only there if the title is set through the textfield. I couldn’t find anything on this topic just cb.validate(); but that didn’t really help me. I also tried doing this.add(cb); when the button ks pressed but it also didn’t work.

JavaScript

Advertisement

Answer

Short answer

Swing is lazy, it won’t automatically update the UI when you can it, instead, when you’re done, you need to trigger a new layout and paint pass.

Add…

JavaScript

after this.add(cb);

Long answer

Don’t extend from JFrame, you’re not adding any new functionality to the class and you’re simply locking yourself into a single use case.

Avoid null layouts, they aren’t helping you. Instead, make the time to understand how to use the layout management API which is provided. See Laying Out Components Within a Container

If you’re aiming to produce a list of values, then you should consider making use of either a JTable or JList.

See How to Use Tables and How to Use Lists for more details

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement