2016-09-06 55 views
0

我在更新列表视图中的项目时遇到问题,我试图通过编号更改列表视图中的字体颜色。JavaFx ListView <Float>设置所有项目的字体颜色

my listView

我的意思是,如果数字是< 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

你为什么使用Float.parseFloat(number.getClass()。toString())'? 'number.getClass()。toString()'的输出是一个字符串形式的'class java.lang.Float',并且使用''Float.parseFloat()''方法将会抛出'NumberFormatException' 。也许这就是错误:你应该只写'if(number <6.0f)'。 – aleb2000

+0

这是一个错误,但它没有奏效,堆栈跟踪表示存在空指针异常。 –

+0

你可以请张贴堆栈跟踪吗? – aleb2000

回答

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()方法。

+0

检查数字是否有效,但仍然无法获得文字颜色 –

+0

嗯...在这一点上,我不知道什么可能会导致问题,我会做一些测试,看看我是否找到一种方法做这个工作。 – aleb2000

+0

@UmbertoPalazzini我做了一些测试,发现单元格的文本刚刚消失。问题是你没有添加任何文本到单元格,为了做到这一点,你需要调用ListCell的'setText()'方法。 – aleb2000

相关问题