Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. public class MoneyFormatCell extends ListCell<Number> {
  2.  
  3. public MoneyFormatCell() { }
  4.  
  5. @Override protected void updateItem(Number item, boolean empty) {
  6. // calling super here is very important - don't skip this!
  7. super.updateItem(item, empty);
  8.  
  9. // format the number as if it were a monetary value using the
  10. // formatting relevant to the current locale. This would format
  11. // 43.68 as "$43.68", and -23.67 as "-$23.67"
  12. setText(item == null ? "" : NumberFormat.getCurrencyInstance().format(item));
  13.  
  14. // change the text fill based on whether it is positive (green)
  15. // or negative (red). If the cell is selected, the text will
  16. // always be white (so that it can be read against the blue
  17. // background), and if the value is zero, we'll make it black.
  18. if (item != null) {
  19. double value = item.doubleValue();
  20. setTextFill(isSelected() ? Color.WHITE :
  21. value == 0 ? Color.BLACK :
  22. value < 0 ? Color.RED : Color.GREEN);
  23. }
  24. }
  25. }
  26. This class could then be used inside a ListView as such:
  27. ObservableList<Number> money = ...;
  28. final ListView<Number> listView = new ListView<Number>(money);
  29. listView.setCellFactory(new Callback<ListView<Number>, ListCell<Number>>() {
  30. @Override public ListCell<Number> call(ListView<Number> list) {
  31. return new MoneyFormatCell();
  32. }
  33. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement