2017-05-09 95 views
0

我有這個TableView我想在每一行選擇要獲取ID列的值時使用方法嗎?如何從TableView列中獲取值

ObservableList<Patient> selected,fromAll; 
    fromAll = tvAll.getItems(); 
    selected = tvAll.getSelectionModel().getSelectedItems(); 
    selected.forEach(tcID.getText()); 

這是我的代碼:

package application; 
import java.io.IOException; 
import java.net.URL; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ResourceBundle; 

import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
public class ViewAll implements Initializable { 
    @FXML 
    public TableView<Patient> tvAll; 
    @FXML 
    public TableColumn<Patient, Integer> tcID; 
    @FXML 
    public TableColumn<Patient,String > tcFName; 
    @FXML 
    public TableColumn<Patient,String > tcMName; 
    @FXML 
    public TableColumn<Patient,String > tcLName; 
    @FXML 
    public TableColumn<Patient,Integer > tcAge; 
    @FXML 
    public TableColumn<Patient, String > tcGender; 
    @FXML 
    public TableColumn<Patient, String > tcMartualSt; 
    @FXML 
    public TableColumn<Patient , String > tcAddres1; 
    @FXML 
    public TableColumn<Patient, String > tcAdress2; 
    @FXML 
    public TableColumn<Patient,Integer > tcPhoneNumber; 
    @FXML 
    public TableColumn<Patient,String > tcDate; 

public ObservableList<Patient> data=FXCollections.observableArrayList(); 

@Override 
public void initialize(URL location, ResourceBundle resourcees) { 
    // TODO Auto-generated method stub 

    try{ 
     String sql="SELECT * FROM `patient4` WHERE 1"; 
     Connection con=DBInfo.getConnectio(); 
     PreparedStatement ps= (PreparedStatement) con.prepareStatement(sql); 
     ResultSet rs= ps.executeQuery(); 

     while(rs.next()){ 

      data.add(new Patient(rs.getInt(1),rs.getString(2), rs.getString(3), rs.getString(4),rs.getInt(5), rs.getString(6),rs.getString(7), rs.getString(8), rs.getString(9), rs.getInt(10), rs.getString(11))); 

     } 
     con.close(); 
    }catch(SQLException e){ 
     e.printStackTrace(); 
    } 



tcID.setCellValueFactory(new PropertyValueFactory<Patient,Integer>("id")); 
tcFName.setCellValueFactory(new PropertyValueFactory<Patient ,String >("fName")); 
tcMName.setCellValueFactory(new PropertyValueFactory<Patient ,String >("mName")); 
tcLName.setCellValueFactory(new PropertyValueFactory<Patient ,String >("lName")); 
tcAge.setCellValueFactory(new PropertyValueFactory<Patient,Integer>("Age")); 
tcGender.setCellValueFactory(new PropertyValueFactory<Patient ,String >("Gender")); 
tcMartualSt.setCellValueFactory(new PropertyValueFactory<Patient ,String >("MartualSt")); 
tcAddres1.setCellValueFactory(new PropertyValueFactory<Patient ,String >("Adress1")); 
tcAdress2.setCellValueFactory(new PropertyValueFactory<Patient ,String >("Adress2")); 
tcPhoneNumber.setCellValueFactory(new PropertyValueFactory<Patient,Integer>("Phone number")); 
tcDate.setCellValueFactory(new PropertyValueFactory<Patient ,String >("Date")); 

    tvAll.setItems(data); 

} 
enter code here 
public void selected(ActionEvent e)throws IOException,SQLException{ 

    ObservableList<Patient> selected,fromAll; 
    fromAll = tvAll.getItems(); 
    selected = tvAll.getSelectionModel().getSelectedItems(); 
    selected.forEach(tcID.getText()); 
} 

}

回答

0

選擇模型的getSelectedItems()方法使您可以選擇的項目清單:即代表每個所選行Patient的List。所以你可以從他們每個人得到身份證號碼:

public void selected(ActionEvent e)throws IOException,SQLException{ 

    ObservableList<Patient> selected; 
    selected = tvAll.getSelectionModel().getSelectedItems(); 
    for (Patient patient : selected) { 
     int id = patient.getId(); 
     // do whatever you need with id... 
    } 
} 
+0

謝謝James_D你真的救了我的命,再次感謝 –