简体   繁体   中英

JButton Value If Else Conditon with JSpinner doesn't work properly

I'm trying to make a timer that has a jbutton called Short Break and a jbutton called Customize .

By clicking the default Short Break button the time will be set to 5.00 minutes.

But the user can customize the time with the help of JSpinner by clicking on the customize button. But if the user wants to reduce the short break time by clicking on the customize button, then clicking on the short break button after customizing will show the customized time.

But after customizing, the customized time is shown, but clicking on the short break button without customizing does not show the default value. Means that the if condition only work but the else condition doesn't.

My JSpinner Code:

spinnerShortBreak.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            value2 = Integer.parseInt(String.valueOf(spinnerShortBreak.getValue()));
        }
    });

The Short Break button Code:

btnShortBreak.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            timer.stop();
            second = 00;
            minute = 05;
            if (spinnerShortBreak !=null && spinnerShortBreak.getModel().getValue().equals(value2)) {
                lblMinute.setText(String.valueOf(value2));
            }
            else if(spinnerShortBreak == null && spinnerShortBreak.getValue() == null) {                    
                btnStartStop.setText("Start");
                lblMinute.setText("05");
                lblSecond.setText("00");
            }
        }
    });

I find a solution. If i check the value is equal to 0 or greater than 0 the code will work perfectly.

here the button code:

btnShortBreak.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            timer.stop();
            if(value2 > 0){
                second = 00;
                lblMinute.setText("0"+String.valueOf(value2));
                btnStartStop.setText("Start");
                lblSecond.setText("00");
            }
            else if(value2 == 0) {
                second = 00;
                minute = 05;
                btnStartStop.setText("Start");
                lblMinute.setText("05");
                lblSecond.setText("00");
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM