2016-04-27 17 views
7

Lựa chọn nhiều bảngView trả về một trong các đối tượng được chọn là rỗng. Điều này không xảy ra mỗi lần nhưng xảy ra hầu hết các lần khi tôi cố gắng chọn hai hàng trong bảng. similar to the issue defined in this questionjavafx8 TableView Multiselection trả về một trong các mục được chọn là null

Cách tốt nhất để tái tạo vấn đề là, hãy thử chọn hai hàng tuần tự. 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> 

Bộ điều khiển:

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; 
    } 
} 

mẫu:

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; 

} 
+0

Tôi đã cập nhật mã một chút với:.. personsTable.getSelectionModel() getSelectedItems() addListener (InvalidationListener mới() {..code vào đây để in tên nếu không null khác "null tìm" ..}; Dễ dàng sao chép bằng cách chọn hai hàng bằng cách sử dụng "Shift + Click" rồi chọn hàng thứ hai bằng cách nhấp vào nó. – DVarga

+1

Đó là một lỗi trong JavaFX (8u60 +). Xem câu trả lời của tôi [ở đây] (http://stackoverflow.com/a/38207246/4185959). – Itai

Trả lời

0

1: thêm một nhà máy di động mặc định để TableView bạn

2: ghi đè lên một trong hai updateItem hoặc updateIndex

hy vọng nó helpls

+0

Có thể xin vui lòng cho một số gợi ý về những gì cần phải được thực hiện trong phương pháp updateItem ghi đè? – user68883

+0

Tại sao điều này giải quyết được vấn đề? Một nhà máy di động chỉ quan tâm đến cách các mục được hiển thị, không phải là mô hình lựa chọn. – Itai

+0

Bạn có thể xây dựng nó tốt hơn không? Tôi không thể thấy bất kỳ lý do nào tại sao điều này sẽ hữu ích – dinhokz