From 51779fb868d8aa1eb645266ee795a4b47bf8c523 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Sun, 12 Jun 2016 16:38:54 +0200 Subject: [PATCH] Table: better focus and duplicate/delete/swap --- .../jstudy/controller/TableController.java | 25 +++++++++++++------ .../java/es/kauron/jstudy/model/TestItem.java | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/TableController.java b/src/main/java/es/kauron/jstudy/controller/TableController.java index 5091089..40453fa 100644 --- a/src/main/java/es/kauron/jstudy/controller/TableController.java +++ b/src/main/java/es/kauron/jstudy/controller/TableController.java @@ -11,6 +11,7 @@ import javafx.fxml.Initializable; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; +import javafx.scene.control.SelectionMode; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.stage.Modality; @@ -34,13 +35,18 @@ public class TableController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { + table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); answerCol.setCellValueFactory(e -> e.getValue().answerProperty()); questionCol.setCellValueFactory(e -> e.getValue().questionProperty()); -// editButton.disableProperty().bind(table.getSelectionModel().selectedIndexProperty().lessThan(0)); -// swapButton.disableProperty().bind(table.getSelectionModel().selectedIndexProperty().lessThan(0)); -// deleteButton.disableProperty().bind(table.getSelectionModel().selectedIndexProperty().lessThan(0)); -// duplicateButton.disableProperty().bind(table.getSelectionModel().selectedIndexProperty().lessThan(0)); -// testSelectionButton.disableProperty().bind(table.getSelectionModel().selectedIndexProperty().lessThan(0)); + bindButtons(); + } + + private void bindButtons() { + editButton.disableProperty().bind(table.getSelectionModel().selectedIndexProperty().lessThan(0)); + swapButton.disableProperty().bind(table.getSelectionModel().selectedIndexProperty().lessThan(0)); + deleteButton.disableProperty().bind(table.getSelectionModel().selectedIndexProperty().lessThan(0)); + duplicateButton.disableProperty().bind(table.getSelectionModel().selectedIndexProperty().lessThan(0)); + testSelectionButton.disableProperty().bind(table.getSelectionModel().selectedIndexProperty().lessThan(0)); } void setData(List list, Controller controller) { @@ -97,16 +103,21 @@ public class TableController implements Initializable { item.questionProperty().set(item.getAnswer()); item.answerProperty().set(question); } + table.requestFocus(); } @FXML private void onDuplicateAction(ActionEvent event) { - table.getItems().addAll(table.getSelectionModel().getSelectedItems()); + for (int i : table.getSelectionModel().getSelectedIndices()) + table.getItems().add(new TestItem(table.getItems().get(i))); + table.requestFocus(); } @FXML private void onDeleteAction(ActionEvent event) { - table.getItems().removeAll(table.getSelectionModel().getSelectedItems()); + for (int i : table.getSelectionModel().getSelectedIndices()) + table.getItems().remove(i); + table.requestFocus(); } @FXML diff --git a/src/main/java/es/kauron/jstudy/model/TestItem.java b/src/main/java/es/kauron/jstudy/model/TestItem.java index f3d68e4..88fa56f 100644 --- a/src/main/java/es/kauron/jstudy/model/TestItem.java +++ b/src/main/java/es/kauron/jstudy/model/TestItem.java @@ -24,7 +24,7 @@ public class TestItem { } public TestItem(String question, String answer) { - this(new SimpleStringProperty(question), new SimpleStringProperty(answer)); + this(new SimpleStringProperty(new String(question)), new SimpleStringProperty(new String(answer))); } public String getQuestion() {