dimanche 31 janvier 2016

javaFX - tableview getting selected items

How can I get a selected item in a row from tableview in javaFX with Sqlite Database.

I am now using this to get the Index of the row:

(...)

@FXML
    private ObservableList<UserData> data;

@FXML
    private TableView table;

@FXML
    private void pressTableAction() {
        System.out.println("Selected index: " +table.getSelectionModel().getSelectedIndex());
    }

(...)

 public void initialize (URL url, ResourceBundle rb) {
        try {
            con = DataBaseConnection.getConnected();
            stat = con.createStatement();
            data = FXCollections.observableArrayList();
            ResultSet rs = con.createStatement().executeQuery("SELECT * FROM person");
            while (rs.next()) {
                data.add(new UserData(rs.getInt("p_id"), rs.getString("Name")));
            }
            column1.setCellValueFactory(new PropertyValueFactory("p_id"));
            column2.setCellValueFactory(new PropertyValueFactory("Name"));
            table.setItems(null);
            table.setItems(data);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error on Building Data");
        }






Public static class UserData {

        private IntegerProperty p_id;
        private StringProperty Name;


        private UserData(Integer p_id, String Name) {
            this.p_id = new SimpleIntegerProperty (p_id);
            this.Name = new SimpleStringProperty(Name);

        }

        public IntegerProperty p_idProperty() {
            return p_id;
        }

        public StringProperty NameProperty() {
            return Name;
        }
}

My db looks like this:

CREATE TABLE person (p_id INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR(30) NOT NULL);

How can I get the p_id or the Name of the row I clicked?

@FXML
    private void pressTableAction() {
    System.out.println("Selected index: " + table.getSelectionModel().getSelectedIndex());
    System.out.println("Selected p_id: " + ????)
    }

Aucun commentaire:

Enregistrer un commentaire