Nolan Kr Nolan Kr - 10 days ago 7
Java Question

How do I enable a jButton when jSlider is 100?

I'm so sorry for creating a new one cloning the older one (which I deleted by the way). I had the title wrong from the previous one and I'm really sorry for that! Anyways,

I want it so that my button will be enabled once my slider reaches 100. These are my only codes so far I think I've got the logic but I really don't know if I'm using these correctly.

I ran my jFrame and set the slider to 100 but the button just won't enable when jFrame is at max.

boolean login;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (login = true){
jButton1.setEnabled(true);
}
}

private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
int slidervalue = jSlider1.getValue();
if (slidervalue==100){
login = true;
}
}


I've already set the button to start with a
false
.

Answer

You need to call jButton1.setEnabled(true) within your slider's ChangeListener. Just setting the login field to true will have no magical effect. Also, I'd use if (sliderValue >= 100) in case the ChangeListener isn't triggered at exactly 100 (assuming that your slider goes above 100 -- if not, ignore that).

e.g.,

private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {                                      
    int slidervalue = jSlider1.getValue();
    if (slidervalue >= 100){
       login = true;
       jButton1.setEnabled(true);
    } else {
       // if you want to de-activate things if the slider is < 100
       login = false;
       jButton1.setEnabled(false);
    }
}