我在更新列表視圖中的項目時遇到問題,我試圖通過編號更改列表視圖中的字體顏色。JavaFx ListView <Float>設置所有項目的字體顏色
我的意思是,如果數字是< 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
你爲什麼使用Float.parseFloat(number.getClass()。toString())'? 'number.getClass()。toString()'的輸出是一個字符串形式的'class java.lang.Float',並且使用''Float.parseFloat()''方法將會拋出'NumberFormatException' 。也許這就是錯誤:你應該只寫'if(number <6.0f)'。 – aleb2000
這是一個錯誤,但它沒有奏效,堆棧跟蹤表示存在空指針異常。 –
你可以請張貼堆棧跟蹤嗎? – aleb2000