从tableview javafx中选择时获取字符串

时间:2014-09-26 20:27:18

标签: java text javafx tableview

因此,在基本形式中,我想从tableview中获取所选文本。 我有我的SetCoachFXML,其中有tableview,其中包含一些数据。接下来我选择了按钮。当我点击选择按钮时,如何从tableview中获取所选文本?

http://imgur.com/wA6n792

我尝试了here的建议,但我一无所获。

这是我的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;
}

}

1 个答案:

答案 0 :(得分:0)

我认为正在发生的事情是当您点击按钮时,您对所选单元格失去了关注,这意味着当您尝试检索数据时,没有任何反应。

您需要做的是确保当您单击按钮时,单元格/行仍处于选中状态。

然后你可以做类似的事情:

// To retrieve
Person person = (Person)taview.getSelectionModel().getSelectedItem();
System.out.println(person.getName());