TableView编辑单元格很奇怪

时间:2016-05-31 14:54:11

标签: user-interface tableview javafx-8

我正在构建一种日记,使我的用户能够收集特定数据,即成员号和每个成员的数量。一旦m / no正确并且用户按Tab键,该成员的名称应该显示,这将使数量开始编辑。

我找到了this示例并将其作为起点。

我将把我的作品发布为SSCCE,但是你要知道我所取得的成就是错误的。当我键入第一条记录时

    public class CollectionTVEdit extends Application{
        private TableView<Collection> table = new    TableView<Collection>();
        private ObservableList<Collection> collectionList = FXCollections.<Collection>observableArrayList();
        ListProperty<Collection> collectionListProperty = new SimpleListProperty<>();

        public static void main(String[] args) {
            launch(args);
        }

        @Override
       public void start(Stage primaryStage) throws Exception {
            primaryStage.setTitle("Table View Sample");
            primaryStage.setWidth(450);
            primaryStage.setHeight(550);

           final Label label = new Label("Address Book");
           label.setFont(new Font("Arial", 20));

           table.setEditable(true);
           Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() {
            public TableCell call(TableColumn p) {
                    return new EditingCell();
            }
    };
    Callback<TableColumn, TableCell> floatCellFactory = new Callback<TableColumn, TableCell>() {
        public TableCell call(TableColumn p) {
                return new FloatEditingCell();
        }
    };

    TableColumn colMNO = new TableColumn("M/NO");
    colMNO.setMinWidth(100);
    colMNO.setCellValueFactory(new PropertyValueFactory("mno"));
    colMNO.setCellFactory(cellFactory);
    colMNO.setOnEditCommit(new EventHandler<CellEditEvent<Collection, String>>() {
        @Override
        public void handle(CellEditEvent<Collection, String> t) {
            ((Collection) t.getTableView().getItems()
                            .get(t.getTablePosition().getRow()))
                            .setMno(t.getNewValue());
        }
    });

    TableColumn colName = new TableColumn("Name");
    colName.setMinWidth(100);
    colName.setCellValueFactory(new PropertyValueFactory("name"));
    colName.setEditable(false);

    TableColumn colQuantity = new TableColumn("Quantity");
    colQuantity.setMinWidth(100);
    colQuantity.setCellValueFactory(new PropertyValueFactory("quantity"));
    colQuantity.setCellFactory(floatCellFactory);
    colQuantity.setOnEditCommit(new EventHandler<CellEditEvent<Collection, Float>>() {
        @Override
        public void handle(CellEditEvent<Collection, Float> t) {
            ((Collection) t.getTableView().getItems()
                            .get(t.getTablePosition().getRow()))
                            .setQuantity(t.getNewValue());
        }
    });

    collectionListProperty.set(collectionList);
    table.itemsProperty().bindBidirectional(collectionListProperty);
    table.getColumns().addAll(colMNO, colName, colQuantity);
    collectionList.add(new Collection());
    collectionList.add(new Collection());

    Scene scene = new Scene(new BorderPane(table), 880, 600);
    primaryStage.setScene(scene);
    primaryStage.show();
}


class FloatEditingCell extends TableCell<Collection, Float> {

    private TextField textField;

    public FloatEditingCell() {
    }

    @Override
    public void startEdit() {
        if (!isEmpty()) {
            super.startEdit();
            if (textField == null) {
                createTextField();
            }

            setGraphic(textField);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            // textField.selectAll();
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    textField.requestFocus();
                    textField.selectAll();
                }
            });
        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();

        setText(""+(Float) getItem());
        setGraphic(null);
    }

    @Override
    public void updateItem(Float item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());
                }
                setGraphic(textField);
                setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            } else {
           //                                    table.getColumns().get(0).setVisible(false);//////////////my new line
           //                                    table.getColumns().get(0).setVisible(true);///////////////my new line
                setText(getString());
                setContentDisplay(ContentDisplay.TEXT_ONLY);
            }
        }
    }

    private void createTextField() {
        textField = new TextField(getString());
        textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()
                * 2);

        textField.focusedProperty().addListener(
                new ChangeListener<Boolean>() {
                    @Override
                    public void changed(
                            ObservableValue<? extends Boolean> arg0,
                            Boolean arg1, Boolean arg2) {
                        if (!arg2) {
                            commitEdit(Float.parseFloat(textField.getText()));
                        }
                    }
                });

        textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent t) {
                if (t.getCode() == KeyCode.ENTER) {
                    commitEdit(Float.parseFloat(textField.getText()));
                } else if (t.getCode() == KeyCode.ESCAPE) {
                    cancelEdit();
                } else if (t.getCode() == KeyCode.TAB) {
                    commitEdit(Float.parseFloat(textField.getText()));
                    TableColumn nextColumn = getNextColumn(!t.isShiftDown());
                if (nextColumn != null) {
                    getTableView().edit(getTableRow().getIndex(),
                            nextColumn);
                }

                }
            }

        });
    }

    private String getString() {
        return getItem() == null ? "" : getItem().toString();
    }

    private TableColumn<Collection, ?> getNextColumn(boolean forward) {
        List<TableColumn<Collection, ?>> columns = new ArrayList<>();
        for (TableColumn<Collection, ?> column : getTableView().getColumns()) {
            columns.addAll(getLeaves(column));
        }
        // There is no other column that supports editing.
        if (columns.size() < 2) {
            return null;
        }
        int currentIndex = columns.indexOf(getTableColumn());
        int nextIndex = currentIndex;
        if (forward) {
            nextIndex++;
            if (nextIndex > columns.size() - 1) {
                nextIndex = 0;
                                    table.getItems().add(new Collection());
            }
        } else {
            nextIndex--;
            if (nextIndex < 0) {
                nextIndex = columns.size() - 1;
            }
        }
        return columns.get(nextIndex);
    }

    private List<TableColumn<Collection, ?>> getLeaves(
            TableColumn<Collection, ?> root) {
        List<TableColumn<Collection, ?>> columns = new ArrayList<>();
        if (root.getColumns().isEmpty()) {
            // We only want the leaves that are editable.
            if (root.isEditable()) {
                columns.add(root);
            }
            return columns;
        } else {
            for (TableColumn<Collection, ?> column : root.getColumns()) {
                columns.addAll(getLeaves(column));
            }
            return columns;
        }
    }
}

class EditingCell extends TableCell<Collection, String> {

    private TextField textField;

    public EditingCell() {
    }

    @Override
    public void startEdit() {
        if (!isEmpty()) {
            super.startEdit();
            if (textField == null) {
                createTextField();
            }

            setGraphic(textField);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            // textField.selectAll();
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    textField.requestFocus();
                    textField.selectAll();
                }
            });
        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();

        setText((String) getItem());
        setGraphic(null);
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());
                }
                setGraphic(textField);
                setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            } else {
                setText(getString());
                setContentDisplay(ContentDisplay.TEXT_ONLY);
            }
        }
    }

    private void createTextField() {
        textField = new TextField(getString());
        textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()
                * 2);

        textField.focusedProperty().addListener(
                new ChangeListener<Boolean>() {
                    @Override
                    public void changed(
                            ObservableValue<? extends Boolean> arg0,
                            Boolean arg1, Boolean arg2) {
                        if (!arg2) {
                            commitEdit(textField.getText());
                        }
                    }
                });

        textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent t) {
                if (t.getCode() == KeyCode.ENTER) {
                    commitEdit(textField.getText());
                } else if (t.getCode() == KeyCode.ESCAPE) {
                    cancelEdit();
                } else if (t.getCode() == KeyCode.TAB) {
                    commitEdit(textField.getText());
                    TableColumn nextColumn = getNextColumn(!t.isShiftDown());
                if (nextColumn != null) {
                    getTableView().edit(getTableRow().getIndex(),
                            nextColumn);
                }

                }
            }

        });
    }

    private String getString() {
        return getItem() == null ? "" : getItem().toString();
    }

    private TableColumn<Collection, ?> getNextColumn(boolean forward) {
        List<TableColumn<Collection, ?>> columns = new ArrayList<>();
        for (TableColumn<Collection, ?> column : getTableView().getColumns()) {
            columns.addAll(getLeaves(column));
        }
        // There is no other column that supports editing.
        if (columns.size() < 2) {
            return null;
        }
        int currentIndex = columns.indexOf(getTableColumn());
        int nextIndex = currentIndex;
        if (forward) {
            nextIndex++;
            if (nextIndex > columns.size() - 1) {
                nextIndex = 0;
                                    table.getItems().add(new Collection());
            }
        } else {
            nextIndex--;
            if (nextIndex < 0) {
                nextIndex = columns.size() - 1;
            }
        }
        return columns.get(nextIndex);
    }

    private List<TableColumn<Collection, ?>> getLeaves(
            TableColumn<Collection, ?> root) {
        List<TableColumn<Collection, ?>> columns = new ArrayList<>();
        if (root.getColumns().isEmpty()) {
            // We only want the leaves that are editable.
            if (root.isEditable()) {
                columns.add(root);
            }
            return columns;
        } else {
            for (TableColumn<Collection, ?> column : root.getColumns()) {
                columns.addAll(getLeaves(column));
            }
            return columns;
        }
    }
}

public class Collection {
    private int id;
    private String mno;
    private String name;
    private float quantity;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMno() {
        return mno;
    }

    public void setMno(String mno) {
        this.mno = mno;
    }

    public String getName() {
        return name;
    }

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

    public float getQuantity() {
        return quantity;
    }

    public void setQuantity(float quantity) {
        this.quantity = quantity;
    }
}

}

我想要实现的是用户能够在绑定到列表的表中插入数据。稍后可能点击按钮我选择列表并持续到db。现在的问题是表格表现奇怪。不保存数据,有时插入数据显示在不同的单元格中

0 个答案:

没有答案