Populating TableView In JavaFX with TableView created in FXML File

后端 未结 2 612
鱼传尺愫
鱼传尺愫 2020-12-20 05:21

I am a newbie in JavaFX and also in java. Guys can you please figure out this code if what is lacking because I can already print the items from the database using the print

相关标签:
2条回答
  • 2020-12-20 06:06

    The Property Value Factory doesn't match with any getters and setters function in your bean. So if you set the cell value factory as the next:

    colID.setCellValueFactory(new PropertyValueFactory<studentInfo, Integer>("id"));
    

    The property value factory will look for the function getId(), setId() and idProperty(). The last one returns the property itself, that you have in your bean. Check the API of PropertyValueFactory and the tutorial of Table View to go deep on this.

    Also do what agonist_ says, you are creating a the column twice.

    There are a couple things. You are setting the CellValueFactory every time you click on the button. You need to put it in the initialize not in the button event.

    And the function getAllstudentInfo() will block your GUI, because the query it's blocking. You should do this in a background thread.

    So replace your tblViewer.getItems().setAll(getAllstudentInfo()); to something like:

    Service<ObservableList<studentInfo>> service = new Service<ObservableList<studentInfo>>() {                 
    @Override
    protected Task<ObservableList<studentInfo>> createTask() {
        return new Task<ObservableList<studentInfo>>() {                            
            @Override
            protected ObservableList<studentInfo> call() throws Exception {
                return FXCollections.observableArrayList(getAllstudentInfo());
            }
        };
    }
    };
    tblViewer.itemsProperty().bind(service.valueProperty());        
    service.start();
    

    Take a look on concurrency tutorial of JavaFX 2 sure will help you.

    Hope it helps.

    0 讨论(0)
  • 2020-12-20 06:13

    Start by not doing new instance on your FXML import. Just

    @FXML
    private TableColumn<String> colContact;
    
    0 讨论(0)
提交回复
热议问题