JavaFx ListView <float>设置所有项目的字体颜色

时间:2016-09-06 14:03:45

标签: java css listview nullpointerexception

我在更新列表视图中的项目时遇到问题,我正在尝试使用数字更改列表视图中的字体颜色。

my listView

我的意思是如果数字是&lt; 6.0,具体项目字体颜色应为红色,否则应为绿色。 这是我试过的代码:

public void loadMarksListView(String key){
    ObservableList<Float> items = FXCollections.observableArrayList();
    items.addAll(subjectsMarks.get(key));
    marksListView.setItems(items);

    marksListView.setCellFactory(new Callback<ListView<Float>, ListCell<Float>>() {
        @Override
        public ListCell<Float> call(ListView<Float> param) {
            return new ColoredCell();
        }
    });
}

static class ColoredCell extends ListCell<Float>{
    @Override
    public void updateItem(Float number, boolean empty) {
        super.updateItem(number, empty);

        if (Float.parseFloat(number.getClass().toString()) < 6.0f){
            this.setTextFill(Color.RED);
        }
        else{
            this.setTextFill(Color.GREEN);
        }
    }
}

修改 堆栈跟踪

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at main.Controller$ColoredCell.updateItem(Controller.java:88)
at main.Controller$ColoredCell.updateItem(Controller.java:83)
at javafx.scene.control.ListCell.updateItem(ListCell.java:480)
at javafx.scene.control.ListCell.access$300(ListCell.java:72)
at javafx.scene.control.ListCell$4.invalidated(ListCell.java:299)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.control.ListCell.setListView(ListCell.java:305)
at javafx.scene.control.ListCell.updateListView(ListCell.java:494)
at com.sun.javafx.scene.control.skin.ListViewSkin.createCell(ListViewSkin.java:292)
at com.sun.javafx.scene.control.skin.ListViewSkin.lambda$new$366(ListViewSkin.java:99)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1777)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1879)
at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2528)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:381)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)

处理完成,退出代码为0

1 个答案:

答案 0 :(得分:1)

问题在于,在updateItem()方法中,您必须检查项目是否为空:

static class ColoredCell extends ListCell<Float>{
    @Override
    public void updateItem(Float number, boolean empty) {
        super.updateItem(number, empty);

        if(number == null || empty) {
            setText(null);
            setGraphic(null);
        } else {

            setText(number.toString()); // This line is very important!

            if (number < 6.0f){
                this.setTextFill(Color.RED);
            } else {
                this.setTextFill(Color.GREEN);
            }
        }
    }
}

编辑: 您必须手动设置单元格的文本,因为您正在重新定义单元格的呈现方式,因此您必须添加所需的所有图形组件和值(例如,如果您想为每个标记添加图标,则由于事实你不再使用默认单元格,你必须在ColoredCell类中调用setGraphic()方法。