ListView的自定义CellFactory

ListView的自定义CellFactory,listview,javafx,javafx-8,Listview,Javafx,Javafx 8,我想为ListView实现自定义CellFactory: import javafx.application.Application; import static javafx.application.Application.launch; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.EventHandler; import jav

我想为ListView实现自定义CellFactory:

  import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class MainApp extends Application
{
    ListView<String> list = new ListView<String>();
    ObservableList<String> data = FXCollections.observableArrayList(
        "Option 1",
        "Option 2",
        "Option 3",
        "Option 4",
        "Option 5"
    );

    @Override
    public void start(Stage stage)
    {
        VBox box = new VBox();
        Scene scene = new Scene(box, 200, 200);
        stage.setScene(scene);
        stage.setTitle("ListViewSample");
        box.getChildren().addAll(list);
        VBox.setVgrow(list, Priority.ALWAYS);

        list.setItems(data);

        list.setCellFactory(new Callback<ListView<String>, ListCell<String>>()
        {
            @Override
            public ListCell<String> call(ListView<String> list)
            {
                return new ColorRectCell();
            }
        }
        );

        stage.show();
    }

    class ColorRectCell extends ListCell<String>
    {
        public ColorRectCell()
        {
            addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
            {
                @Override
                public void handle(MouseEvent mouseEvent)
                {
                    if (mouseEvent.getButton().equals(MouseButton.PRIMARY) && !isEmpty())
                    {
                        if (mouseEvent.getClickCount() == 2)
                        {
                            System.out.println(">>>>>>>>> Clicked");
                        }
                    }
                }
            });
        }

        @Override
        public void updateItem(String item, boolean empty)
        {
            super.updateItem(item, empty);
            if (item != null)
            {
                setText(item);

            }
        }
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}
导入javafx.application.application;
导入静态javafx.application.application.launch;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.ListCell;
导入javafx.scene.control.ListView;
导入javafx.scene.input.MouseButton;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.Priority;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入javafx.util.Callback;
公共类MainApp扩展应用程序
{
ListView列表=新建ListView();
ObservableList data=FXCollections.observableArrayList(
“选择1”,
“选择2”,
“选择3”,
“选择4”,
“选择5”
);
@凌驾
公众假期开始(阶段)
{
VBox box=新的VBox();
场景=新场景(框,200200);
舞台场景;
stage.setTitle(“ListViewSample”);
box.getChildren().addAll(列表);
setVgrow(列表、优先级、始终);
列表.设置项(数据);
list.setCellFactory(新回调()
{
@凌驾
公共ListCell调用(ListView列表)
{
返回新的ColorRectCell();
}
}
);
stage.show();
}
类ColorRectCell扩展了ListCell
{
公共ColorRectCell()
{
addEventHandler(鼠标点击MouseEvent.MOUSE_,新建EventHandler())
{
@凌驾
公共无效句柄(MouseEvent MouseEvent)
{
if(mouseEvent.getButton().equals(MouseButton.PRIMARY)&&!isEmpty())
{
if(mouseEvent.getClickCount()==2)
{
System.out.println(“>>>>>>>>单击”);
}
}
}
});
}
@凌驾
public void updateItem(字符串项,布尔值为空)
{
super.updateItem(项,空);
如果(项!=null)
{
setText(项目);
}
}
}
公共静态void main(字符串[]args)
{
发射(args);
}
}

我只想在单击包含文本的行时激活双击侦听器,如果该行为空,则跳过该行。在CellFactory中实现双击监听器的正确方法是什么?

在cell中注册监听器(与在listView中注册监听器相比)有什么问题?在你提出了所有问题之后,你应该已经学会了如何使用它,对吗:-)@kleopatra我找不到任何好的例子来说明如何将监听器插入定制的CellFactory。??cellfactory创建一个单元,该单元是一个节点,可以具有ui事件的处理程序/过滤器:创建后在工厂中注册,或者在自定义事件的构造函数中注册cell@kleopatra代码已更新。顺便问一下,你能提出一些更好的代码实现吗?重复:在自定义单元格的构造函数中