< prev index next >

modules/javafx.controls/src/main/java/javafx/scene/control/Cell.java

Print this page




 122  *
 123  *     public MoneyFormatCell() {    }
 124  *
 125  *     @Override protected void updateItem(Number item, boolean empty) {
 126  *         // calling super here is very important - don't skip this!
 127  *         super.updateItem(item, empty);
 128  *
 129  *         // format the number as if it were a monetary value using the
 130  *         // formatting relevant to the current locale. This would format
 131  *         // 43.68 as "$43.68", and -23.67 as "-$23.67"
 132  *         setText(item == null ? "" : NumberFormat.getCurrencyInstance().format(item));
 133  *
 134  *         // change the text fill based on whether it is positive (green)
 135  *         // or negative (red). If the cell is selected, the text will
 136  *         // always be white (so that it can be read against the blue
 137  *         // background), and if the value is zero, we'll make it black.
 138  *         if (item != null) {
 139  *             double value = item.doubleValue();
 140  *             setTextFill(isSelected() ? Color.WHITE :
 141  *                 value == 0 ? Color.BLACK :
 142  *                 value < 0 ? Color.RED : Color.GREEN);
 143  *         }
 144  *     }
 145  * }</pre>
 146  *
 147  * This class could then be used inside a ListView as such:
 148  *
 149  * <pre>
 150  * ObservableList&lt;Number&gt; money = ...;
 151  * final ListView&lt;Number&gt; listView = new ListView&lt;Number&gt;(money);
 152  * listView.setCellFactory(new Callback&lt;ListView&lt;Number&gt;, ListCell&lt;Number&gt;&gt;() {
 153  *     @Override public ListCell&lt;Number&gt; call(ListView&lt;Number&gt; list) {
 154  *         return new MoneyFormatCell();
 155  *     }
 156  * });</pre>
 157  *
 158  * In this example an anonymous inner class is created, that simply returns
 159  * instances of MoneyFormatCell whenever it is called. The MoneyFormatCell class
 160  * extends {@link ListCell}, overriding the
 161  * {@link #updateItem(java.lang.Object, boolean) updateItem} method. This method
 162  * is called whenever the item in the cell changes, for example when the user




 122  *
 123  *     public MoneyFormatCell() {    }
 124  *
 125  *     @Override protected void updateItem(Number item, boolean empty) {
 126  *         // calling super here is very important - don't skip this!
 127  *         super.updateItem(item, empty);
 128  *
 129  *         // format the number as if it were a monetary value using the
 130  *         // formatting relevant to the current locale. This would format
 131  *         // 43.68 as "$43.68", and -23.67 as "-$23.67"
 132  *         setText(item == null ? "" : NumberFormat.getCurrencyInstance().format(item));
 133  *
 134  *         // change the text fill based on whether it is positive (green)
 135  *         // or negative (red). If the cell is selected, the text will
 136  *         // always be white (so that it can be read against the blue
 137  *         // background), and if the value is zero, we'll make it black.
 138  *         if (item != null) {
 139  *             double value = item.doubleValue();
 140  *             setTextFill(isSelected() ? Color.WHITE :
 141  *                 value == 0 ? Color.BLACK :
 142  *                 value &lt; 0 ? Color.RED : Color.GREEN);
 143  *         }
 144  *     }
 145  * }</pre>
 146  *
 147  * This class could then be used inside a ListView as such:
 148  *
 149  * <pre>
 150  * ObservableList&lt;Number&gt; money = ...;
 151  * final ListView&lt;Number&gt; listView = new ListView&lt;Number&gt;(money);
 152  * listView.setCellFactory(new Callback&lt;ListView&lt;Number&gt;, ListCell&lt;Number&gt;&gt;() {
 153  *     @Override public ListCell&lt;Number&gt; call(ListView&lt;Number&gt; list) {
 154  *         return new MoneyFormatCell();
 155  *     }
 156  * });</pre>
 157  *
 158  * In this example an anonymous inner class is created, that simply returns
 159  * instances of MoneyFormatCell whenever it is called. The MoneyFormatCell class
 160  * extends {@link ListCell}, overriding the
 161  * {@link #updateItem(java.lang.Object, boolean) updateItem} method. This method
 162  * is called whenever the item in the cell changes, for example when the user


< prev index next >