如何从TableView列获取值

时间:2017-05-09 07:00:26

标签: java javafx

我选择每一行获取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());
}

}

1 个答案:

答案 0 :(得分:0)

选择模型的getSelectedItems()方法为您提供所选项目的列表:即表示每个选定行的Patient列表。所以你可以从每个人那里得到id:

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...
    }
}
相关问题