How to show Leading Zeros in a Label with Binding to an Integer Property

19 Views Asked by At

I using two labels to show Minute and Seconds created using Scenebuilder in Control class. There is a start button also, which starts a timer. After every sec, time is shown in Sec and Minute labels. But, when Sec is less than 10, only one digit is shown, I want to display like 01, 02, 04 ..mean with leading zero also and max two digits. How to do this ?

   public class Control implements Initializable 
  {
  @FXML Label lblMin,lblSec;
  @FXML Button btnStart;
  private IntegerProperty remSec = new SimpleIntegerProperty(0);
  private IntegerProperty remMin = new SimpleIntegerProperty(0);
  static Timer timerobj ;
  static TimerTask timertask;
  //*******************
    @Override
  public void initialize(URL arg0, ResourceBundle arg1) 
  {
        timertask = new timerTask();
        // other binding and initializatinons
    }                              

   @FXML
   private void eventStart(ActionEvent event)
   {
      timerobj = new Timer();
      timerobj.schedule(timertask, 1000, 1000);
    }

    class timerTask extends TimerTask{
    @Override   
    public void run(){
    
    Platform.runLater(() -> {
     
          if(getremSecValue()>0)
          {
         setremSecValue(getremSecValue()-1);
          }
         else {          
                 setremMinValue(getremMinValue()-1);
                 if(getremMinValue()==0){       
                 time_over = true;
                } 
                else setremSecValue(59);
              }
            });
        }
     }
     public IntegerProperty getremSec() {
      return remSec;
     }
     public Integer getremSecValue() {
      return remSec.get();
     }

     public void setremSec(IntegerProperty remSec) {
         this.remSec = remSec;
       }
      public void setremSecValue(Integer remSec) {
         this.remSec.set(remSec);
        }

        public void setremMin(IntegerProperty remMin) {
            this.remMin = remMin;
        }
        public void setremMinValue(Integer remMin) {
        this.remMin.set(remMin);
        }

        public IntegerProperty getremMin() {
         return remMin;
        }
        public Integer getremMinValue() {
         return remMin.get();
           }
       }

The application code is in another class.

        public class Spm extends Application {
       static Control controller;
      //  after loading the spm.fxml file ... creating a scene view, did following
      controller = (Control) loader.getController();
      // binding 
  
    StringBinding str = controller.getremSec().asString();        
    controller.lblSec.textProperty().bind(str);
    str = controller.getremMin().asString();
    controller.lblMin.textProperty().bind(str);
 }

The Main class is:

     public class Main {    
     public static void main(String[] args) {
    //launch(args);
    Application.launch(Spm.class);
      }
    }
0

There are 0 best solutions below