When I put a lot of items (by the button) and select the last item, then it is also selected cells above the last row ,which are empty.To reproduce run the follow code open the window, select a lot of items by mouse (like excel) push delete button and finally push a lot of times add button to add Items.Now select in the last row one or multiple cells and then you can see that are selected also cells above.In order to see that do not produce a lot of additional items, so the vertical bar do not have to be shown.
1)FXML FILE :
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="385.0" prefWidth="432.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tableviewsample.FXMLDocumentController">
<children><TableView fx:id="table" editable="true" layoutX="19.0" layoutY="58.0" prefHeight="200.0" prefWidth="394.0" AnchorPane.bottomAnchor="127.0" AnchorPane.leftAnchor="19.0" AnchorPane.rightAnchor="19.0" AnchorPane.topAnchor="58.0">
<columns>
<TableColumn fx:id="c1" text="C1" /><TableColumn fx:id="c2" prefWidth="131.0" text="C2" /><TableColumn fx:id="c3" prefWidth="63.0" text="C3" /><TableColumn fx:id="c4" prefWidth="31.0" text="C4" /><TableColumn fx:id="c5" prefWidth="75.0" text="C5" /><TableColumn fx:id="c6" prefWidth="75.0" text="C6" /><TableColumn fx:id="c7" prefWidth="75.0" text="C7" /><TableColumn fx:id="c8" prefWidth="75.0" text="C8" /><TableColumn fx:id="c9" prefWidth="75.0" text="C9" />
<TableColumn fx:id="c10" prefWidth="70.0" text="C10" /><TableColumn fx:id="c11" prefWidth="75.0" text="C11" /><TableColumn fx:id="c12" prefWidth="75.0" text="C12" /><TableColumn fx:id="c13" prefWidth="75.0" text="C13" /><TableColumn fx:id="c14" prefWidth="75.0" text="C14" />
</columns>
</TableView><Button fx:id="additem" layoutX="286.0" layoutY="25.0" mnemonicParsing="false" onAction="#addItm" text="Add" /><Button fx:id="deleteItem" layoutX="347.1875" layoutY="25.0" mnemonicParsing="false" onAction="#removeItm" text="Delete" />
</children>
</AnchorPane>
2) Java code
package helloworld.dtl;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
public class FXMLDocumentController extends Application implements Initializable {
@FXML Button addItem;
@FXML Button removeItem;
@FXML private TableView<Item> table;
@FXML private TableColumn<Item, String> c1;
@FXML private TableColumn<Item, String> c2;
@FXML private TableColumn<Item, String> c3;
@FXML private TableColumn<Item, String> c4;
@FXML private TableColumn<Item, String> c5;
@FXML private TableColumn<Item, String> c6;
@FXML private TableColumn<Item, String> c7;
@FXML private TableColumn<Item, String> c8;
@FXML private TableColumn<Item, String> c9;
@FXML private TableColumn<Item, String> c10;
@FXML private TableColumn<Item, String> c11;
@FXML private TableColumn<Item, String> c12;
@FXML private TableColumn<Item, String> c13;
@FXML private TableColumn<Item, String> c14;
private ObservableList<Item> items = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
for (int i = 0; i < 4; i++) {
items.add(new Item());
}
c1.setCellValueFactory(new PropertyValueFactory("c1"));
c2.setCellValueFactory(new PropertyValueFactory("c2"));
c3.setCellValueFactory(new PropertyValueFactory("c3"));
c4.setCellValueFactory(new PropertyValueFactory("c4"));
c5.setCellValueFactory(new PropertyValueFactory("c5"));
c6.setCellValueFactory(new PropertyValueFactory("c6"));
c7.setCellValueFactory(new PropertyValueFactory("c7"));
c8.setCellValueFactory(new PropertyValueFactory("c8"));
c9.setCellValueFactory(new PropertyValueFactory("c9"));
c10.setCellValueFactory(new PropertyValueFactory("c10"));
c11.setCellValueFactory(new PropertyValueFactory("c11"));
c12.setCellValueFactory(new PropertyValueFactory("c12"));
c13.setCellValueFactory(new PropertyValueFactory("c13"));
c14.setCellValueFactory(new PropertyValueFactory("c14"));
table.setItems(items);
table.getSelectionModel().setCellSelectionEnabled(true);
}
@FXML
public void addItm(ActionEvent event){
table.getItems().add(new Item());
}
@FXML
public void removeItm(ActionEvent event){
ArrayList<Item> itemIdentifiers = new ArrayList<>();
ObservableList<Item> itemsToRemove = table.getSelectionModel().getSelectedItems();
if (itemsToRemove != null) {
for (Item itemModelToRemove : itemsToRemove) {
//if we select two rows we have to put once the itemIdentifier
if (!itemIdentifiers.contains(itemModelToRemove)) {
itemIdentifiers.add(itemModelToRemove);
}
}
RemoveItems(itemIdentifiers);
}
}
public void RemoveItems(ArrayList<Item> list){
for(Item item:list){
table.getItems().remove(item);
}
}
@Override public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
public static class Item {
private SimpleStringProperty c1=new SimpleStringProperty("c1");
private SimpleStringProperty c2=new SimpleStringProperty("c2");
private SimpleStringProperty c3=new SimpleStringProperty("c3");
private SimpleStringProperty c4=new SimpleStringProperty("c4");
private SimpleStringProperty c5=new SimpleStringProperty("c5");
private SimpleStringProperty c6=new SimpleStringProperty("c6");
private SimpleStringProperty c7=new SimpleStringProperty("c7");
private SimpleStringProperty c8=new SimpleStringProperty("c8");
private SimpleStringProperty c9=new SimpleStringProperty("c9");
private SimpleStringProperty c10=new SimpleStringProperty("c10");
private SimpleStringProperty c11=new SimpleStringProperty("c11");
private SimpleStringProperty c12=new SimpleStringProperty("c12");
private SimpleStringProperty c13=new SimpleStringProperty("c13");
private SimpleStringProperty c14=new SimpleStringProperty("c14");
public Item(){ }
public StringProperty c1Property() {
return c1;
}
public StringProperty c2Property() {
return c2;
}
public StringProperty c3Property() {
return c3;
}
public StringProperty c4Property() {
return c4;
}
public StringProperty c5Property() {
return c5;
}
public StringProperty c6Property() {
return c6;
}
public StringProperty c7Property() {
return c7;
}
public StringProperty c8Property() {
return c8;
}
public StringProperty c9Property() {
return c9;
}
public StringProperty c10Property() {
return c10;
}
public StringProperty c11Property() {
return c11;
}
public StringProperty c12Property() {
return c12;
}
public StringProperty c13Property() {
return c13;
}
public StringProperty c14Property() {
return c14;
}
}
}
1)FXML FILE :
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="385.0" prefWidth="432.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tableviewsample.FXMLDocumentController">
<children><TableView fx:id="table" editable="true" layoutX="19.0" layoutY="58.0" prefHeight="200.0" prefWidth="394.0" AnchorPane.bottomAnchor="127.0" AnchorPane.leftAnchor="19.0" AnchorPane.rightAnchor="19.0" AnchorPane.topAnchor="58.0">
<columns>
<TableColumn fx:id="c1" text="C1" /><TableColumn fx:id="c2" prefWidth="131.0" text="C2" /><TableColumn fx:id="c3" prefWidth="63.0" text="C3" /><TableColumn fx:id="c4" prefWidth="31.0" text="C4" /><TableColumn fx:id="c5" prefWidth="75.0" text="C5" /><TableColumn fx:id="c6" prefWidth="75.0" text="C6" /><TableColumn fx:id="c7" prefWidth="75.0" text="C7" /><TableColumn fx:id="c8" prefWidth="75.0" text="C8" /><TableColumn fx:id="c9" prefWidth="75.0" text="C9" />
<TableColumn fx:id="c10" prefWidth="70.0" text="C10" /><TableColumn fx:id="c11" prefWidth="75.0" text="C11" /><TableColumn fx:id="c12" prefWidth="75.0" text="C12" /><TableColumn fx:id="c13" prefWidth="75.0" text="C13" /><TableColumn fx:id="c14" prefWidth="75.0" text="C14" />
</columns>
</TableView><Button fx:id="additem" layoutX="286.0" layoutY="25.0" mnemonicParsing="false" onAction="#addItm" text="Add" /><Button fx:id="deleteItem" layoutX="347.1875" layoutY="25.0" mnemonicParsing="false" onAction="#removeItm" text="Delete" />
</children>
</AnchorPane>
2) Java code
package helloworld.dtl;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
public class FXMLDocumentController extends Application implements Initializable {
@FXML Button addItem;
@FXML Button removeItem;
@FXML private TableView<Item> table;
@FXML private TableColumn<Item, String> c1;
@FXML private TableColumn<Item, String> c2;
@FXML private TableColumn<Item, String> c3;
@FXML private TableColumn<Item, String> c4;
@FXML private TableColumn<Item, String> c5;
@FXML private TableColumn<Item, String> c6;
@FXML private TableColumn<Item, String> c7;
@FXML private TableColumn<Item, String> c8;
@FXML private TableColumn<Item, String> c9;
@FXML private TableColumn<Item, String> c10;
@FXML private TableColumn<Item, String> c11;
@FXML private TableColumn<Item, String> c12;
@FXML private TableColumn<Item, String> c13;
@FXML private TableColumn<Item, String> c14;
private ObservableList<Item> items = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
for (int i = 0; i < 4; i++) {
items.add(new Item());
}
c1.setCellValueFactory(new PropertyValueFactory("c1"));
c2.setCellValueFactory(new PropertyValueFactory("c2"));
c3.setCellValueFactory(new PropertyValueFactory("c3"));
c4.setCellValueFactory(new PropertyValueFactory("c4"));
c5.setCellValueFactory(new PropertyValueFactory("c5"));
c6.setCellValueFactory(new PropertyValueFactory("c6"));
c7.setCellValueFactory(new PropertyValueFactory("c7"));
c8.setCellValueFactory(new PropertyValueFactory("c8"));
c9.setCellValueFactory(new PropertyValueFactory("c9"));
c10.setCellValueFactory(new PropertyValueFactory("c10"));
c11.setCellValueFactory(new PropertyValueFactory("c11"));
c12.setCellValueFactory(new PropertyValueFactory("c12"));
c13.setCellValueFactory(new PropertyValueFactory("c13"));
c14.setCellValueFactory(new PropertyValueFactory("c14"));
table.setItems(items);
table.getSelectionModel().setCellSelectionEnabled(true);
}
@FXML
public void addItm(ActionEvent event){
table.getItems().add(new Item());
}
@FXML
public void removeItm(ActionEvent event){
ArrayList<Item> itemIdentifiers = new ArrayList<>();
ObservableList<Item> itemsToRemove = table.getSelectionModel().getSelectedItems();
if (itemsToRemove != null) {
for (Item itemModelToRemove : itemsToRemove) {
//if we select two rows we have to put once the itemIdentifier
if (!itemIdentifiers.contains(itemModelToRemove)) {
itemIdentifiers.add(itemModelToRemove);
}
}
RemoveItems(itemIdentifiers);
}
}
public void RemoveItems(ArrayList<Item> list){
for(Item item:list){
table.getItems().remove(item);
}
}
@Override public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
public static class Item {
private SimpleStringProperty c1=new SimpleStringProperty("c1");
private SimpleStringProperty c2=new SimpleStringProperty("c2");
private SimpleStringProperty c3=new SimpleStringProperty("c3");
private SimpleStringProperty c4=new SimpleStringProperty("c4");
private SimpleStringProperty c5=new SimpleStringProperty("c5");
private SimpleStringProperty c6=new SimpleStringProperty("c6");
private SimpleStringProperty c7=new SimpleStringProperty("c7");
private SimpleStringProperty c8=new SimpleStringProperty("c8");
private SimpleStringProperty c9=new SimpleStringProperty("c9");
private SimpleStringProperty c10=new SimpleStringProperty("c10");
private SimpleStringProperty c11=new SimpleStringProperty("c11");
private SimpleStringProperty c12=new SimpleStringProperty("c12");
private SimpleStringProperty c13=new SimpleStringProperty("c13");
private SimpleStringProperty c14=new SimpleStringProperty("c14");
public Item(){ }
public StringProperty c1Property() {
return c1;
}
public StringProperty c2Property() {
return c2;
}
public StringProperty c3Property() {
return c3;
}
public StringProperty c4Property() {
return c4;
}
public StringProperty c5Property() {
return c5;
}
public StringProperty c6Property() {
return c6;
}
public StringProperty c7Property() {
return c7;
}
public StringProperty c8Property() {
return c8;
}
public StringProperty c9Property() {
return c9;
}
public StringProperty c10Property() {
return c10;
}
public StringProperty c11Property() {
return c11;
}
public StringProperty c12Property() {
return c12;
}
public StringProperty c13Property() {
return c13;
}
public StringProperty c14Property() {
return c14;
}
}
}