-3

I belive question in title is detailed enough.

Are there tools that allow to do this easily in javafx?

As per comment: IntegerProperty is abstraction in JavaFX. Here I am using SimpleIntegerProperty.

I'd like to convert integer binding to said time format (as binding).

Main problem here is how can I do it with relatively short code (I expect fx provides some tricks for this - like for everything else).

Ernio
  • 948
  • 10
  • 25
  • 3
    The question is not detailed enough. No idea what the IntegerProperty is intended to represent. – scottb Jan 24 '17 at 22:20
  • Also it's never a good idea to include a integral part of the question only in the title... – fabian Jan 25 '17 at 19:11
  • @Ernio What does your `IntegerProperty`represent? seconds? milliseconds? can you give an example of what you want the property to translate as? – MikaelF Feb 02 '17 at 03:45
  • Question is clear enough. OP is asking how to format an SimpleIntegerProperty binding to a Label. If the SimpleIntegerProperty contained the value "2", then a binding to the label such as `hoursLabel.textProperty().bind(hoursSimpleIntProp.asString());` would have the label display "2". But OP wants "02" for the hoursLabel. – FriskySaga Mar 27 '20 at 18:10

2 Answers2

1

I am guessing that your IntegerProperty represents a number of seconds? If so, siddhadev's answer on How to convert Milliseconds to “X mins, x seconds” in Java? gives a great way to format an integer representing a time unit into a String. All that is left to do is to add a listener to your property and set the formatted String from there.

public static String formatStringToTime(int seconds){
    return String.format("%02d:%02d:%02d", TimeUnit.SECONDS.toHours(time),
            TimeUnit.SECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes((TimeUnit.SECONDS.toHours(time))),
            time - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(time)));
}

...

//main method
IntegerProperty prop = new SimpleIntegerProperty();
prop.addListener((ob, ov, nv) -> yourObject.setString(formatStringToTime(nv));
Community
  • 1
  • 1
MikaelF
  • 3,518
  • 4
  • 20
  • 33
0

Suppose your SimpleIntegerProperty is set to a single-digit value such as 9. To bind this value to your Label with a 0 prefixed, simply say:

label.textProperty().bind(simpleIntegerProperty.asString("%02d"));

This will achieve your desired format: hh:mm:ss

FriskySaga
  • 409
  • 1
  • 8
  • 19