简体   繁体   English

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

[英]How to receive data in particular row of tableView in javafx?

I am working on rfid and I am receiving data in tableView from database mysql which matches the UID of my rfid tag. 我正在rfid上工作,我正在从tablemysql中接收来自数据库mysql的数据,该数据与我的rfid标记的UID相匹配。 But when I tap the another RFid tag, the previous data is overwritten by the new one. 但是,当我点击另一个RFid标签时,以前的数据将被新的RFid标签覆盖。 But I want the new data in next row of tableview. 但是我想要新数据在tableview的下一行中。 This is my Controller code: 这是我的控制器代码:

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

    }
}

DetectBean : DetectBean:

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

Image of output: 输出图像:

not displaying data in student ID column 在学生证列中不显示数据

I think the Problem is: 我认为问题是:

tableView.setItems(list); 

resets the list. 重置列表。

try instead : 请尝试:

tableview.getItems().addAll(list); 

Alternatively only set an ObservableList in the initialize method and change that list directly in you query. 或者,仅在initialize方法中设置一个ObservableList,然后直接在查询中更改该列表。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM