Default Table Model in JavaFX

情到浓时终转凉″ 提交于 2020-01-15 09:09:10

问题


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:


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);
    }
});


来源:https://stackoverflow.com/questions/17316328/default-table-model-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!