0

So in basic form I want to get selected text from tableview. I have my SetCoachFXML in which I have tableview, with some data in it. Next to that I have choose button. How can I get selected text from tableview when I click on choose button?

https://i.stack.imgur.com/O3dtD.jpg

I tried suggestion from here but I get nothing.

Here is my setcoach controller class:

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

and my Coaches class:

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

}

Community
  • 1
  • 1
tadija
  • 23
  • 3
  • 6

1 Answers1

0

I think what's happening is when you click on the button, your loosing focus on the selected cell which means when you try to retrieving data, nothing happens.

What you need to do is make sure that when you click on the button, the cell/row is still selected.

Then you can do something like:

// To retrieve
Person person = (Person)taview.getSelectionModel().getSelectedItem();
System.out.println(person.getName());   
breaktop
  • 1,899
  • 4
  • 37
  • 58