This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 10 - Pages: 1 - Last Post: 18 Jul 23, 19:57 Last Post By: AndreWolff Threads: [ Previous | Next ]
AndreWolff

Posts: 1,156
Registered: 14-Dec-2007
How to enable / disable a check-box in Netbeans
Posted: 17 Jul 23, 11:38
 
  Click to reply to this thread Reply
I use in my NetBeans GUI next two check-marks and statemonitor:
JCheckBox skipIndexPage = new JCheckBox("Skip thumbnails pages"); 
 
JCheckBox autoPlaySlideShow = new JCheckBox("Start slide show automatically if index page is skipped");
 
new StateMonitor() {
  public void onChange() {
      autoPlaySlideShow.setEnabled(skipIndexPage.isSelected());
   }
}.add(skipIndexPage).done();
This works fine if I check or clear check-box skipIndexPage

However if I open the project and go to the slide settings page, autoPlaySlideShow is enabled while skipIndexPage is cleared. This is incorrect, the autoPlaySlideShow check-box should be be disabled if skipIndexPage is cleared and autoPlaySlideShow should be be enabled if skipIndexPage is checked.

What initialze code should I use to get that done?
davidekholm

Posts: 3,655
Registered: 18-Oct-2002
Re: How to enable / disable a check-box in Netbeans
Posted: 17 Jul 23, 13:42   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
Your code looks correct. I've tested in on Minimal skin, and it behaves. It's the "done()" call that does the initialization of the state.

I don't think it matters, but as you're using Netbeans and hence Java, then you can use the shorter Lambda based syntax:
StateMonitor.monitoring(skipIndexPage).onChange(c -> autoPlaySlideShow.setEnabled(skipIndexPage.isSelected()));
For these when-selected-enable cases, you can also use the even shorter convenience methods of ComponentUtilities Your code then becomes:
ComponentUtilities.ifSelectedEnable(skipIndexPage, autoPlaySlideShow);
JeffTucker

Posts: 7,956
Registered: 31-Jan-2006
Re: How to enable / disable a check-box in Netbeans
Posted: 17 Jul 23, 13:54   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Or, since it can tackle complex conditions and multiple target fields, use enable:
new StateMonitor() {
	public void onChange() {
		enable(skipIndexPage.isSelected(), autoPlaySlideShow);
	}
}.add(skipIndexPage).done();
AndreWolff

Posts: 1,156
Registered: 14-Dec-2007
Re: How to enable / disable a check-box in Netbeans
Posted: 17 Jul 23, 14:38   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
Well David and Jeff, thanks for your replies, but your proposed solutions do not solve my problem.

It is an initiialization problem, if you open the settings , there is no change event, so the code is not executed,

I need code to initialise a check-box as disabled. I tried this:
                            autoPlaySlideShow.setEnabled(false);
                            autoPlaySlideShow.setEnabled(skipIndexPage.isSelected());
                            new StateMonitor() {
                                public void onChange() {
                                    autoPlaySlideShow.setEnabled(skipIndexPage.isSelected());                                 
                                }
                            }.add(skipIndexPage).done();
but that does also not fix my problem.

Does there exists a setDiisabled function?

Edited by: AndreWolff on 17 Jul 2023, 16:06
JeffTucker

Posts: 7,956
Registered: 31-Jan-2006
Re: How to enable / disable a check-box in Netbeans
Posted: 17 Jul 23, 17:17   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
That's what the .done() is for. It sets the enabling on autoPlaySlideShow upon skin loading, taking the skin default for skipIndexPage, or taking the saved value of skipIndexPage from the project file. The .done() triggers the onChange, in effect.

It has worked this way for over a decade. No other code required.
AndreWolff

Posts: 1,156
Registered: 14-Dec-2007
Re: How to enable / disable a check-box in Netbeans
Posted: 17 Jul 23, 17:42   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
Well my project file contains this
skin.skipIndexPage=false
but if I open this project, the check-box autoPlaySlideShow on the Slide Options page is not grayed.

But it is not important, so I stop looking to the cause of this problem.
Thanks for the explanation.
AndreWolff

Posts: 1,156
Registered: 14-Dec-2007
Re: How to enable / disable a check-box in Netbeans
Posted: 17 Jul 23, 21:04   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
I could solve this problem by moving the check-box "Start slide show automatically if index page is skipped" outside the slide show parameters panel.
davidekholm

Posts: 3,655
Registered: 18-Oct-2002
Re: How to enable / disable a check-box in Netbeans
Posted: 18 Jul 23, 11:18   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
Good. In general, adding System.out.println("...") statements to spots of interest, like setting up the StateMonitor may guide you to the cause of the problem. Perhaps the StateMonitor code wasn't even called?
AndreWolff

Posts: 1,156
Registered: 14-Dec-2007
Re: How to enable / disable a check-box in Netbeans
Posted: 18 Jul 23, 12:25   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Good idea, I never used that in NetBeans. I tried this:
                            new StateMonitor() {
                                public void onChange() {
                                    autoPlaySlideShow.setEnabled(skipIndexPage.isSelected() && enableSlideShow.isSelected() ); 
                                    System.out.println("2 enableSlideShow: " + enableSlideShow ); 
                                }
                            }.add(skipIndexPage).add(enableSlideShow).done();
But I don't get true of false, but this:
2 enableSlideShow: javax.swing.JCheckBox[,9,144,119x23,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@36101618,flags=360,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],paintBorder=false,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Enable slide show]
How can I print just the value of enableSlideShow ?
davidekholm

Posts: 3,655
Registered: 18-Oct-2002
Re: How to enable / disable a check-box in Netbeans
Posted: 18 Jul 23, 17:13   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
enableSlideShow.isSelected()
AndreWolff

Posts: 1,156
Registered: 14-Dec-2007
Re: How to enable / disable a check-box in Netbeans
Posted: 18 Jul 23, 19:57   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
Thnks!
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums