我的Tableview中只填充了一列

时间:2016-05-17 04:40:45

标签: mysql javafx

我正在从mysql数据库填充我的tableview,但只有列ID是唯一填充的。

我的主要:

        public void populate() throws Exception{

     ObservableList<userdata1> data = FXCollections.observableArrayList(); 

     tableView();

     try{   
            String query = "select * from members";        
           ps = new Connect().connectDatabase1();
           rs = ps.executeQuery(query);         
            while(rs.next()){             
            data.add(new userdata1(rs.getInt(1),rs.getString(2),rs.getInt(3)));            
           tblView.setItems(data);   
           }


       }catch(Exception e){
           System.out.print("asdqweasd");

       }
}


        public void tableView()throws Exception{            
        tblView.getItems().clear();        
        tblView.getColumns().clear();   
        rs = ps.executeQuery("SELECT * FROM members");      
        ObservableList<userdata1> data = FXCollections.observableArrayList();       
        TableColumn column1 = new TableColumn("ID");    
        column1.setMinWidth(85);     
        column1.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<>("ID"));        
         TableColumn column2 = new TableColumn("Name");
        column2.setMinWidth(565);
        column2.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<>("comp_name"));   
        TableColumn column3 = new TableColumn("STATUS");    
        column3.setMinWidth(123);     
        column3.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<>("mem_status")); 



        tblView.getColumns().addAll(column1,column2,column3);     

    }    

我的userdata1:

public class userdata1 {
    public SimpleIntegerProperty ID;
     public SimpleStringProperty comp_name;
     public SimpleIntegerProperty mem_status;

    public userdata1(Integer id, String comp_name, Integer mem_status){
           this.ID = new SimpleIntegerProperty(id);
           this.comp_name = new SimpleStringProperty(comp_name);
           this.mem_status = new SimpleIntegerProperty(mem_status);

       }
    public Integer getID() {
            return ID.get();
        }  

          public String getcomp_name(){
             return comp_name.get();
        }

        public Integer getmem_status() {
            return mem_status.get();
        }


        public void setID(Integer id) {

            this.ID.set(id);
        }

        public void setcomp_name(String comp_name ) {
           this.comp_name.set(comp_name);
        }

        public void setmem_status(Integer mem_status) {

            this.mem_status.set(mem_status);
        }
}

数据mem_status和comp_name未填充各自的列

1 个答案:

答案 0 :(得分:0)

由于UserData1已包含Properties,您可以将Property设置为cellValueFactory

public class UserData1 {
    private StringProperty comp_name;
    //additional fields, getters and setters  

    public StringProperty comp_nameProperty() {
        return comp_name;
    }
}


setCellValueFactory(cellData -> cellData.getValue().comp_nameProperty());

如果你想坚持PropertyValueFactory,你必须根据CamelCase惯例访问这些字段:

column2.setCellValueFactory(new PropertyValueFactory<>("comp_name"));


public class UserData1 {
   //...
   public String getComp_name(){
             return comp_name.get();
   }
}