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