如何在javafx中接收tableview的特定行中的数据?

aydmsdu9  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(557)

我正在研究rfid,我正在tableview中从数据库mysql接收数据,它与我的rfid标签的uid相匹配。但是当我点击另一个rfid标签时,以前的数据会被新的数据覆盖。但我希望新数据在tableview的下一行。这是我的控制器代码:

public class detectController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TableView<detectBean> tableView;

    @FXML
    private TextField txtSTID;

    ObservableList<detectBean> list;

    public static SerialPort s1;
    static String temp="";
    static String temp1="";

    static void doAlert(String msg)
    {
        Alert alert=new Alert(AlertType.INFORMATION);
        alert.setTitle("Alert..");
        alert.setContentText(msg);
        alert.show();
    } 

    ObservableList<detectBean> getRecordsFromTableSome(String sID) throws FileNotFoundException
    {
         list=FXCollections.observableArrayList();

        try {
            pst=con.prepareStatement("select * from stuRegis where studentID=?");
            pst.setString(1, sID);

            ResultSet rs=  pst.executeQuery();
            while(rs.next())
            {
                String studentID=rs.getString("studentID");
                String name=rs.getString("name");
                String sroll=rs.getString("sroll");
                String clas=rs.getString("clas");
                String fname=rs.getString("fname");
                String contact=rs.getString("contact");
                String pic = rs.getString("pic");
                FileInputStream photo=new FileInputStream(pic);
                Image image1 = new Image(photo, 100, 100, false, false);
                detectBean bean=new detectBean(studentID, name, sroll, clas, fname, contact, new ImageView(image1));
                list.add(bean);
            }

            } 
        catch (SQLException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;

  }

    /////////////////////////////////////////////////////

    @FXML
    void doFetch(ActionEvent event) throws IOException{

        String a = recall();
        txtSTID.setText(a);
        ObservableList<detectBean> list=getRecordsFromTableSome(a);

        tableView.setItems(list);

    }

    @FXML
    void doComClose(ActionEvent event) {

        if(s1.closePort()){
            doAlert("Port Closed");
            System.out.println("Port closed successFully");
        }else{
            doAlert("Failed to Close Port");
            System.out.println("Failed to close port");
        }
    }

    @FXML
    void doOpenCom(ActionEvent event) {

        port();
    }
    /////////////////////////////////////////////////////

    public static void port() 
    {
        SerialPort[] s=SerialPort.getCommPorts();
        for(SerialPort port:s){
            System.out.println(""+port.getSystemPortName());
            s1=SerialPort.getCommPort(port.getSystemPortName());
            if(s1.openPort()){
                doAlert("Port Opened");
                System.out.println("Port opened successFully ");
            }else{
                doAlert("Failed to Open Port");
                System.out.println("Failed to open port");
            }
        }
        s1.setBaudRate(9600);

    }

    public static String recall() throws IOException
    {   
InputStream is=s1.getInputStream();

        StringBuilder st = new StringBuilder();
        for(int i=0,x=0;true;i++){
    //for (int i =0;i<11;i++){

        st=st.append((char)is.read());

       temp1=st.toString();

       if(temp1.length()==13)
       { System.out.print(temp1);
         //System.out.print(temp);
         //System.out.print(temp1.length());
           break;
       }
       System.out.print(temp1);

}

//System.out.print(""+(char)is.read());

        temp=temp1.substring(0,11);
        System.out.print(temp.length());
    System.gc();
    return temp;

    }

    //////////////////////////////////////////////////////

    PreparedStatement pst;
    Connection con;

    @FXML
    void initialize() throws IOException, FileNotFoundException {

        con=MysqlConnection.doConnect();

        TableColumn<detectBean, String> studentID=new TableColumn<detectBean, String>("Student ID");//Dikhava Title
        studentID.setCellValueFactory(new PropertyValueFactory<>("studentID"));//bean field name
        studentID.setMinWidth(90);

        TableColumn<detectBean, String> name=new TableColumn<detectBean, String>("Name");//Dikhava Title
        name.setCellValueFactory(new PropertyValueFactory<>("name"));//bean field name

        TableColumn<detectBean, String> sroll=new TableColumn<detectBean, String>("Roll No.");//Dikhava Title
        sroll.setCellValueFactory(new PropertyValueFactory<>("sroll"));//bean field name

        TableColumn<detectBean, String> clas=new TableColumn<detectBean, String>("Class");//Dikhava Title
        clas.setCellValueFactory(new PropertyValueFactory<>("clas"));//bean field name

        TableColumn<detectBean, String> fname=new TableColumn<detectBean, String>("Father's Name");//Dikhava Title
        fname.setCellValueFactory(new PropertyValueFactory<>("fname"));//bean field name

        TableColumn<detectBean, String> contact=new TableColumn<detectBean, String>("Contact No.");//Dikhava Title
        contact.setCellValueFactory(new PropertyValueFactory<>("contact"));//bean field name
        contact.setMinWidth(90);

        TableColumn<detectBean, Image> pic=new TableColumn<detectBean, Image>("Image");//Dikhava Title
        pic.setCellValueFactory(new PropertyValueFactory<>("pic"));//bean field name
        pic.setMinWidth(110);  

        tableView.getColumns().clear();
        tableView.getColumns().addAll(studentID,name,sroll,clas,fname,contact,pic);

    }
}

检测Bean:

public class detectBean {

    String studentID;
    String name;
    String sroll;
    String clas;
    String fname;
    String contact;
    ImageView image;

    public detectBean(String studentID, String name, String sroll, String clas, String fname, String contact, ImageView image) {
        super();
        this.studentID = studentID;
        this.name = name;
        this.sroll = sroll;
        this.clas = clas;
        this.fname = fname;
        this.contact = contact;
        this.image = image;

    }

    public String getsID() {
        return studentID;
    }
    public void setsID(String studentID) {
        this.studentID = studentID;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSroll() {
        return sroll;
    }
    public void setSroll(String sroll) {
        this.sroll = sroll;
    }
    public String getClas() {
        return clas;
    }
    public void setClas(String clas) {
        this.clas = clas;
    }
    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public String getContact() {
        return contact;
    }
    public void setContact(String contact) {
        this.contact = contact;
    }
    public ImageView getPic() {
        return image;
    }
    public void setPic(String pic) {
        this.image = image;
    }
}

输出图像:
未在“学生id”列中显示数据

eblbsuwk

eblbsuwk1#

我认为问题在于:

tableView.setItems(list);

重置列表。
请尝试:

tableview.getItems().addAll(list);

或者只在initialize方法中设置一个observeList,并在查询中直接更改该列表。

public class detectController {

    //....

    @FXML
    private TableView<detectBean> tableView;
    ObservableList<detectBean> list;
    ///....

   /////////////////////////////////////////////////////

@FXML
void doFetch(ActionEvent event) throws IOException{

    String a = recall();
    txtSTID.setText(a);
    ObservableList<detectBean> list=getRecordsFromTableSome(a);
    /// here!!!
    // tableView.setItems(list);
    tableView.getItems().addAll(list);

}

相关问题