JavaFX TableView删除问题 [英] JavaFX TableView delete issue

查看:188
本文介绍了JavaFX TableView删除问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaFX中从TableView中删除数据时遇到了一些问题。
TabeView从静态ObservableList接收数据。

I'm having some issues with deleting datas from my TableView in JavaFX. TabeView receives its data from a static ObservableList.

问题:TableView一次显示6行。当我的ObservableList中有6个数据对象并删除其中的1个时,TableView似乎有一些更新问题:

Problem: TableView shows 6 rows at a time. When there are for example 6 data-objects in my ObservableList and i delete 1 of them, the TableView seems to have some update-issues:

错误

如您所见,红色突出显示的行似乎是我的ObservableList
中仍然存在的值的副本。您无法选择此行,因为ObservableList中当然没有现有的
值。删除更多值后,此行为会一直显示:

As you can see, the red highlighted row seems to be a copy of a still existing value in my ObservableList. You cannot select this row cause there is of course no existing value in ObservableList. This behavior keeps appearing after deleting some more values:

输入链接这里的描述

所有突出显示行都不可选,因为我的ObservableList中没有相应的值。只有在删除TableView中的最后一个值/行后,它才会刷新并变为空。

All highlights "rows" are not selectable cause there is no corresponding value in my ObservableList. Only after deleting the last value / row in TableView it is refreshed and becomes empty.

我的ObservableList ist static

My ObservableList ist static

public static ObservableList<ImageData> datas_all;

删除非常简单

@FXML
private TableView<ImageData> imageTable;

...

@FXML
private void handleDeleteImage()
{
    ImageData img_data = imageTable.getSelectionModel().getSelectedItem();
    Main.datas_all.remove(img_data);

    //if(Main.datas_flickr.contains(img_data))
    //  Main.datas_flickr.remove(img_data);

    //Main.db_adapter.deleteImage(new Integer[]{img_data.getKey()});

    //File del_file = new File(img_data.getPath());
    //del_file.delete();
}

TableView由

TableView consists of two rows defined by

    @FXML
private TableColumn<ImageData,Image> columnImage;
@FXML
private TableColumn<ImageData,String> columnName;

...

public void redefineTableView()
{
    columnImage.setCellFactory(new Callback<TableColumn<ImageData, Image>, TableCell<ImageData, Image>>() 
    {
        @Override
        public TableCell<ImageData, Image> call(TableColumn<ImageData, Image> param) 
        {
            final ImageView imgView = new ImageView();
            imgView.setFitHeight(100);
            imgView.setFitWidth(100);

            TableCell<ImageData, Image> cell = new TableCell<ImageData, Image>() 
            {
                public void updateItem(Image image, boolean empty) 
                {
                    if(image != null)
                        imgView.setImage(image);
                }
            };
            cell.setGraphic(imgView);
            return cell;
        }
    });

    columnName.setCellFactory(new Callback<TableColumn<ImageData, String>, TableCell<ImageData, String>>() 
    {
        @Override
        public TableCell<ImageData, String> call(TableColumn<ImageData, String> param) 
        {
            TableCell<ImageData, String> cell = new TableCell<ImageData, String>() 
            {
                private Text text;

                public void updateItem(String string, boolean empty) 
                {
                    super.updateItem(string, empty);

                    if (!isEmpty()) 
                    {
                        text = new Text(string);
                        text.setWrappingWidth(170);
                        setGraphic(text);
                    }
                }
            };

            return cell;
        }
    });

    columnName.setCellValueFactory(new PropertyValueFactory<ImageData, String>("name"));
    columnImage.setCellValueFactory(new PropertyValueFactory<ImageData, Image>("image"));

    imageTable.setItems(Main.datas_all);
}

我已经搜索了这个问题,但似乎没有其他人拥有它。
请帮帮我! =)

I've googled this problem but nobody else seems to have it. Please help me! =)

推荐答案

之前有人问过,但我现在找不到它。问题是您的 updateItem(...)方法无法正确处理图像为空(或单元格为空)的情况。从表中删除项目时就是这种情况。您需要:

This has been asked before, but I can't find it now. The issue is that your updateItem(...) method does not properly handle the case where the image is null (or the cell is empty). This will be exactly the case when you delete an item from the table. You need:

        TableCell<ImageData, Image> cell = new TableCell<ImageData, Image>() 
        {
            public void updateItem(Image image, boolean empty) 
            {
                if(image == null) {
                    setGraphic(null);
                } else {
                    imgView.setImage(image);
                    setGraphic(imgView);
                }
            }
        };
        return cell;

和其他单元工厂类似。

这篇关于JavaFX TableView删除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆