Skip to content

Instantly share code, notes, and snippets.

@k33ptoo
Last active July 1, 2018 07:47
Show Gist options
  • Save k33ptoo/aaa832de6dcf77f082cad2cc63c02acb to your computer and use it in GitHub Desktop.
Save k33ptoo/aaa832de6dcf77f082cad2cc63c02acb to your computer and use it in GitHub Desktop.
Adding data to Tableview JavaFX
import home.model.StudentsModel;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import java.net.URL;
import java.util.ResourceBundle;
public class StudentsController implements Initializable {
@FXML
private TableView<StudentsModel> tbData;
@FXML
public TableColumn<StudentsModel, Integer> studentId;
@FXML
public TableColumn<StudentsModel, String> firstName;
@FXML
public TableColumn<StudentsModel, String> lastName;
@Override
public void initialize(URL location, ResourceBundle resources) {
//make sure the property value factory should be exactly same as the e.g getStudentId from your model class
studentId.setCellValueFactory(new PropertyValueFactory<>("StudentId"));
firstName.setCellValueFactory(new PropertyValueFactory<>("FirstName"));
lastName.setCellValueFactory(new PropertyValueFactory<>("LastName"));
//add your data to the table here.
tbData.setItems(studentsModels);
}
// add your data here from any source
private ObservableList<StudentsModel> studentsModels = FXCollections.observableArrayList(
new StudentsModel(1,"Amos", "Chepchieng"),
new StudentsModel(2,"Keep", "Too"),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment