Default Table Model in JavaFX


Question

I have encountered some problem when trying to select single row from table view in JavaFX.

Here is how I populate my table with data from database:

public void populateCategoryTable() {
    data = FXCollections.observableArrayList();
    try {
        db.getConnection();
        String sql = "SELECT * FROM sm_category";
        ResultSet rs = null;
        // Call readRequest to get the result
        rs = db.readRequest(sql);

        while (rs.next()) {
            ObservableList<String> row = FXCollections.observableArrayList();
            //All the rows are added here dynamically 
            row.add(rs.getString("categoryID"));
            data.add(row);
        }
        viewCategory.setItems(data);
        rs.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
        System.out.println("Error SQL!!!");
        System.exit(0);
    }

    TableColumn id = new TableColumn("ID");
    id.setVisible(false);
    id.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
        public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
            return new SimpleStringProperty(param.getValue().get(0).toString());
        }
    });

    viewCategory.getColumns().addAll(id);

    TableView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //Error here
    TableView.TableViewSelectionModel selectionModel = viewCategory.getSelectionModel();
    ObservableList selectedCells = selectionModel.getSelectedCells();
    TablePosition tablePosition = (TablePosition) selectedCells.get(0);
    int row = tablePosition.getRow(); // yields the row that the currently selected cell is in

}

However, when I tried to insert the setSelectionMode code, there is an error. It tells me that cannot find symbol symbol: method setSelectionMode(int) location: class TableView

I remember when I did table in JavaSwing, I used this to set a model for table: DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();

However, I cannot do this in javaFX. Anybody could help me fix this?

Thanks in advance.

1
1
6/26/2013 9:28:50 AM

Accepted Answer

The default selection mode of tableview is SelectionMode.SINGLE. To change it to multiple try

tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

> what am I trying to do is select a single row from table and get the index

To get selected index:

viewCategory.getSelectionModel().getSelectedIndex();

To listen changes of index:

viewCategory.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
        System.out.println("index changed from " + oldValue + " to " + newValue);
    }
});
0
6/26/2013 11:23:46 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon