tableview - javafx8 TableView Multiselection 将所选项目之一返回为 null

标签 tableview javafx-8 multi-select

TableView 多选将所选对象之一返回为 null。这种情况并非每次都会发生,但大多数时候当我尝试在表中选择两行时会发生。 similar to the issue defined in this question

重现该问题的最佳方法是尝试选择两个连续的行。 FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.StackPane?>




<AnchorPane  xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.test.controller.TestController">
                <children>
                    <TableView fx:id="personsTable" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="35.0">
                        <placeholder>
                            <Label text="" />
                        </placeholder>
                        <columns>
                            <TableColumn text="Name">
                                <cellValueFactory>
                                    <PropertyValueFactory property="name" />
                                </cellValueFactory>
                            </TableColumn>
                            <TableColumn text="Address">
                                <cellValueFactory>
                                    <PropertyValueFactory property="address" />
                                </cellValueFactory>
                            </TableColumn>
                            <TableColumn text="Course">
                                <cellValueFactory>
                                    <PropertyValueFactory property="course" />
                                </cellValueFactory>
                            </TableColumn>
                            <TableColumn text="Country">
                                <cellValueFactory>
                                    <PropertyValueFactory property="country" />
                                </cellValueFactory>
                            </TableColumn>
                        </columns>
                    </TableView>
                </children>
            </AnchorPane>

Controller :

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;



import com.test.model.Person;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableView;


public class TestController implements Initializable {

    @FXML
    TableView<Person> personsTable = new TableView<Person>();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        MenuItem combine = new MenuItem("Select");
        combine.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                List<Person> selectedPersons = new ArrayList<Person>(
                        personsTable.getSelectionModel().getSelectedItems());
                for (Person person : selectedPersons) {
                    System.out.println("  the object is " + person);
                }
            }
        });

        ContextMenu contextMenu = new ContextMenu();
        contextMenu.getItems().add(combine);

        personsTable.setContextMenu(contextMenu);

        personsTable.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        personsTable.setItems(getPendingOrders());
    }

    public ObservableList<Person> getPendingOrders() {
        ObservableList<Person> persons = FXCollections.observableArrayList();
        for (int i = 0; i < 100; i++) {
            Person person = new Person();
            person.setName("Name" + i);
            person.setAddress("Address" + i);
            person.setCountry("Country" + i);
            person.setCourse("Course" + i);
            persons.add(person);

        }

        return persons;
    }
}

型号:

public class Person {

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    private String name;
    private String address;
    private String country;
    private String course;

}

最佳答案

1:向 TableView 添加默认单元格工厂

2:覆盖 updateItemupdateIndex

希望对你有帮助

关于tableview - javafx8 TableView Multiselection 将所选项目之一返回为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36893979/

相关文章:

iOS UITableViewCell EditingStyle 以编程方式设置检查(ON)

qt - TableView一列显示多个角色

java - 将 ComboBox#itemsProperty 绑定(bind)到 Map#keySet

css - 从哪里获得所有 JavaFX CSS 属性名称的列表?

Javafx- 鼠标单击事件可撤消上一次鼠标单击所做的操作

uitableview - missViewControllerAnimated 卡住应用程序

javafx - 单击 javafx 中的空行时清除 TableView 中的选择

php - WordPress 下拉多选选项

javascript - 在 MVC 中设置多选下拉列表的选定值

javascript - jQuery 多选下拉菜单,带有所选项目的关闭按钮