请帮忙!我用javafx FXML创建了一个tableview并填充了它。我还编写了一个代码,以便能够将所选行的值提取到另一个FXML文件中。问题是,当我选择任何行时,它只为我选择的每一行提取表视图第一行的值。下面是填充tableview和行选择的代码。
public class ViewController implements Intializable{
@FXML private TableView<Employee,String>viewEmployee;
@FXML private TableColumn<Employee,String>staff;
@FXML private TableColumn<Employee,String>sur;
@FXML private TableColumn<Employee,String>first;
observableList<Employee>data =FXCollections.observablArrayList();
Connection conn;
PreparedStatement pstmt=null;
ResultSet rs=null;
@Override
public void initialize(URL location,ResourceBundle resources){
try{
Class.forName("org.apache.deby.jdbc.ClientDriver");
conn=DriverManager.getConnection("jdbc:derby://localhost:1527/Employee");
String rest="select * from Employee";
pstmt=conn,prepareStatement(rest);
rs=pstmt.executeQuery();
while(rs.next()){
data.add(new Employee(
rs.getString("staffid"),
rs.getString("surname"),
rs.getString("firstname"),
));
staff.setcellValueFactory(newPropertyValueFactory<>("StaffId"));
viewEmployee.setItems(data);
viewEmployee.getSelectedModel().selectedItemProperty().addListener(new ChangeListener<Employee>(){
@Override
public void changed(ObservableValue<?extends employee> observable,Employee oldValue, Employee newValue){
if (newValue !=null){
try{
Stage stage =new stage();
Parent root =FXMLLoader.load(getClaa().getResource("Staff.fxml")
Scene scene =new Scene(root);
stage.setScene(scene);
stage.show();
}catch(IOException ex){
}
}
}
});
}
}catch (ClassNotFoundException |SQLException ex{
}
}
}