Javafx 2 列表视图问题

Javafx 2 列表视图问题,javafx-2,javafx-8,Javafx 2,Javafx 8,这是我的问题看 public class TempStore { // this is a singleton class public static ObservableList<Filez> files= FXCollections.observableArrayList(); } 公共类TempStore{//这是一个单例类 public static observeList files=FXCollections.observearraylist(); } 现在在我的班

这是我的问题看

public class TempStore { // this is a singleton class

public static ObservableList<Filez> files= FXCollections.observableArrayList();
}
公共类TempStore{//这是一个单例类
public static observeList files=FXCollections.observearraylist();
}
现在在我的班上

ListView<Filez> listfiles = new ListView<>();
listfiles.setItems(TempStore.files);
listfiles.setOrientation(Orientation.VERTICAL);
listfiles.setOnMouseClicked(new EventHandler<Event>() {

            @Override
            public void handle(Event arg0) {
                // TODO Auto-generated method stub
                System.out.println(listfiles.getSelectionModel().getSelectedIndex());

            }
        });
listfiles.setCellFactory((ListView<Filez> l) -> listup);
ListView listfiles=新建ListView();
setItems(TempStore.files);
setOrientation(Orientation.VERTICAL);
setOnMouseClicked(新建EventHandler()){
@凌驾
公共无效句柄(事件arg0){
//TODO自动生成的方法存根
System.out.println(listfiles.getSelectionModel().getSelectedIndex());
}
});
setCellFactory((ListView l)->listup);
编辑——我的liscell类

public class Listupdater extends ListCell<Filez> {


@Override
protected void updateItem(Filez arg0, boolean arg1) {
    // TODO Auto-generated method stub
    super.updateItem(arg0, arg1);
    if(arg0 != null)
    setGraphic(view(arg0));
}

@Override
public void updateIndex(int arg0) {
    // TODO Auto-generated method stub
    super.updateIndex(arg0);        
    setGraphic(view(TempStore.files.get(arg0)));
}

@Override
public void updateSelected(boolean arg0) {
    // TODO Auto-generated method stub
    super.updateSelected(arg0);
    System.out.println("update selected");
}

@Override
public void startEdit() {
    // TODO Auto-generated method stub
    super.startEdit();
}

Node view(Filez arg0){
    GridPane grid = new GridPane();
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(2, 2, 2, 2));

    try {
        System.out.print(arg0.toString());
    } catch (Exception e) {
        e.printStackTrace();
        System.out.print("error caught");
    }

    Label lab = new Label(arg0.getName());      
    grid.add(lab, 1, 1);

    return grid;
 }

}
公共类Listupdater扩展ListCell{
@凌驾
受保护的void updateItem(Filez arg0,布尔arg1){
//TODO自动生成的方法存根
super.updateItem(arg0,arg1);
如果(arg0!=null)
设置图形(视图(arg0));
}
@凌驾
公共void updateIndex(int arg0){
//TODO自动生成的方法存根
super.updateIndex(arg0);
setGraphic(视图(TempStore.files.get(arg0));
}
@凌驾
已选择公共无效更新(布尔值arg0){
//TODO自动生成的方法存根
super.updateSelected(arg0);
System.out.println(“选择更新”);
}
@凌驾
公开作废已启动IT(){
//TODO自动生成的方法存根
super.startEdit();
}
节点视图(Filez arg0){
GridPane grid=新建GridPane();
网格。setHgap(10);
网格设置间隙(10);
网格设置填充(新的插图(2,2,2,2));
试一试{
System.out.print(arg0.toString());
}捕获(例外e){
e、 printStackTrace();
系统输出打印(“错误捕获”);
}
Label lab=新标签(arg0.getName());
网格。添加(实验室,1,1);
返回网格;
}
}
现在我的问题是,我的listview中没有显示任何内容,当我单击listview区域system.out.printIn打印-1。。我的列表不是空的,它总是有项目在里面。。我做错了什么有人能帮我吗

另外,为了添加更多信息,我的listview被放入tabpane中的一个选项卡中。。
如果有人也能向我解释一下如何使用listview,我会感到很满意,我所希望的就是能够以编程方式在listview中放置/删除项目,并在listview中使用节点对单元格进行样式设置。首先,对于单元格不显示的问题,我表示感谢,对于工厂的每次调用,您似乎都返回相同的
ListCell
实现实例。每次都应返回一个新实例:

listfiles.setCellFactory(l -> new Listupdater());
要对列表单元格上的鼠标单击作出反应,我将使用单个单元格注册鼠标侦听器,而不是使用
ListView
。这样,您就可以访问该项,而不必检查选择(这有点麻烦:您无法真正保证在调用侦听器之前设置选择)

因此:

公共类Listupdater扩展ListCell{
公共列表更新程序(){
setOnMouseClicked(事件->
如果(!isEmpty()){
Filez f=getItem();
System.out.println(“单击“+f”);
}
);
}
//…你的方法在这里
}

顺便说一句,您不需要同时实现
updateIndex()
updateItem()
,只需
updateItem()
即可。剩下的方法(
updateSelected()
startEdit()
)似乎是多余的,因为它们只是调用超类实现。它们不应该破坏任何东西,但它们只是冗余代码。

您能修复代码的最后一行吗?什么是列表?哦,对不起。。listup是我的listcell类对象。。先生,我也要寄吗@James_,请编辑您的问题并发布代码。你能解释一下你的处理者应该做什么吗?你真的想在鼠标点击时执行某些操作,还是真的想在选择更改时执行某些操作?ohkay.是的,我想让listview中被点击的项目来处理它。。我所要做的就是创建一个带有自定义节点的列表视图作为一个单元格,同时获取点击的项目并对其进行处理,可能还有更多功能..我正在编辑我的问题@James_我已经编辑完毕@James_D非常感谢先生,像你这样的家伙使stackoverflow,非常感谢.再次感谢James_D。。
public class Listupdater extends ListCell<Filez> {

    public Listupdater() {
        setOnMouseClicked(event -> 
            if (! isEmpty() ) {
                Filez f = getItem();
                System.out.println("Click on " + f);
            }
        );
    }

    // ... your methods here
}