2014-09-26 52 views
0

所以在基本形式,我想從tableview中獲取選定的文本。 我有SetCoachFXML,其中我有tableview,其中有一些數據。在旁邊,我有選擇按鈕。當我點擊選擇按鈕時,如何從tableview中獲取選定的文本?當從tableview javafx中選擇時得到字符串

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

}

+0

你說的'選擇text'意思。你需要獲得所選行的行值嗎? – ItachiUchiha 2014-09-27 14:26:55

+0

我需要選擇一行。在我的圖片示例中,我需要獲取名稱和姓氏值。 – tadija 2014-09-27 20:02:47

+0

如何以及在哪裏調用chooseAction和cancelAction方法?目前你沒有使用它們。 – 2014-09-29 06:04:44

回答

0

我覺得發生了什麼,當你點擊按鈕時,你失去焦點在選中的單元格上,這意味着當您嘗試檢索數據時,沒有任何反應。

你需要做的是確保當你點擊按鈕時,單元格/行仍然被選中。

然後,你可以這樣做:

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