![](/img/trans.png)
[英]How to get the selected index of checkbox from tableview on javafx
[英]get string when selected from tableview javafx
因此,我想以基本形式從tableview中獲取選定的文本。 我有SetCoachFXML,其中有表格視圖,其中包含一些數據。 旁邊,我有選擇按鈕。 單擊選擇按鈕時,如何從表視圖中獲取選定的文本?
我從這里嘗試過建議,但一無所獲。
這是我的setcoach控制器類:
public class SetCoachController implements Initializable {
//Kolone i tabela za prikazivanje trenera
@FXML
private TableColumn<Coaches, String> coachesNameCol;
@FXML
private TableColumn<Coaches, String> coachesLNCol;
@FXML
private TableView<Coaches> coachTable;
@FXML
private Button chooseBtn;
@FXML
private Button cancelBtn;
private ObservableList<Coaches> coachesData;
@Override
public void initialize(URL url, ResourceBundle rb) {
coachesNameCol
.setCellValueFactory(new PropertyValueFactory<Coaches, String>(
"name"));
coachesLNCol
.setCellValueFactory(new PropertyValueFactory<Coaches, String>(
"lastName"));
coachesData = FXCollections.observableArrayList();
coachTable.setItems(coachesData);
coachTable.setEditable(false);
CoachBase.get();
loadCoachesData();
}
//sql upit
public void loadCoachesData() {
try {
ResultSet rs = CoachBase.query("SELECT * FROM CoachTable");
coachesData.clear();
while (rs.next()) {
coachesData.add(new Coaches(rs.getString("Name"), rs.getString("Lastname")));
}
} catch (Exception e) {
System.out.println("" + e.getMessage());
}
}
public void chooseAction(ActionEvent event) {
Coaches coach = (Coaches) coachTable.getSelectionModel().getSelectedItem();
System.out.println(coach.getcoachesName());
}
public void cancelAction(ActionEvent event) {
Stage stage = (Stage) cancelBtn.getScene().getWindow();
stage.close();
}
和我的教練班:
public class Coaches {
private SimpleIntegerProperty id = new SimpleIntegerProperty();
private SimpleStringProperty name = new SimpleStringProperty();
private SimpleStringProperty lastName = new SimpleStringProperty();
private SimpleIntegerProperty age = new SimpleIntegerProperty();
public Coaches(Integer id, String name, String lastName, int age) {
this.name.setValue(name);
this.lastName.setValue(lastName);
this.age.setValue(age);
}
public Coaches(String name, String lastName) {
this.name.setValue(name);
this.lastName.setValue(lastName);
}
public Integer getId() {
if (id == null) {
return 0;
}
return id.getValue();
}
public String getcoachesName() {
if (name != null) {
return "";
}
return name.getValueSafe();
}
public String getlastName() {
if (lastName != null) {
return "";
}
return lastName.getValueSafe();
}
public Integer getAge() {
if (age == null) {
return 0;
}
return age.getValue();
}
public SimpleIntegerProperty IdProperty() {
return id;
}
public SimpleStringProperty nameProperty() {
return name;
}
public SimpleStringProperty lastNameProperty() {
return lastName;
}
public SimpleIntegerProperty ageProperty() {
return age;
}
}
我認為發生的事情是當您單擊按鈕時,您將注意力集中在所選單元格上,這意味着當您嘗試檢索數據時,什么也沒有發生。
您需要做的是確保當您單擊按鈕時,仍選中單元格/行。
然后,您可以執行以下操作:
// To retrieve
Person person = (Person)taview.getSelectionModel().getSelectedItem();
System.out.println(person.getName());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.