2017-05-09 66 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你真的救了我的命,再次感谢 –