获取所选行的第一列值

时间:2013-06-26 13:28:55

标签: java swing javafx tableview

选择行并在第一列中获取索引时遇到了一些问题。我知道你可以在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中获取所选行的第一列值。有人知道吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

以下是适用于您的解决方案..以下代码根据行的选择为您提供第一列值。

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

尝试将此应用于您的代码..

谢谢..