0

我在选择一行并获取第一列中的索引时遇到了一些问题。我知道你可以在 Java Swing 中做到这一点:

jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    // Fire this when selected value changed
    jTable.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        int viewRow = jTable.getSelectedRow();
                        // Get the first column data of the selectedrow
                        int topicID = Integer.parseInt(jTable.getValueAt(
                                viewRow, 0).toString());
}
            });

这就是我填充表格数据的方式:

public void getAndPopulateCategory() {
    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"));
            row.add(rs.getString("categoryName"));
            row.add(rs.getString("categoryDescription"));
            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;
}

我正在尝试获取 javaFX 中选定行的第一列值。有人知道吗?提前致谢。

4

1 回答 1

0

Here is the solution for you.. below code gives you first column value based on selection of row.

 tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
                //Check whether item is selected and set value of selected item to Label
                if (tableview.getSelectionModel().getSelectedItem() != null) {
                    TableView.TableViewSelectionModel selectionModel = tableview.getSelectionModel();
                    ObservableList selectedCells = selectionModel.getSelectedCells();

                    TablePosition tablePosition = (TablePosition) selectedCells.get(0);


                    tablePosition.getTableView().getSelectionModel().getTableView().getId();
                    //gives you selected cell value..
                    Object GetSinglevalue = tablePosition.getTableColumn().getCellData(newValue);

                    getbothvalue = tableview.getSelectionModel().getSelectedItem().toString();
                //gives you first column value..
                    Finalvaluetablerow = getbothvalue.toString().split(",")[0].substring(1);
                    System.out.println("The First column value of row.." + Finalvaluetablerow);
                }
            }
        });

Try and apply this to your code..

Thanks..

于 2014-03-12T05:43:46.313 回答