1  import javafx.application.Application;
  2  import javafx.beans.property.SimpleBooleanProperty;
  3  import javafx.beans.property.SimpleDoubleProperty;
  4  import javafx.beans.property.SimpleStringProperty;
  5  import javafx.collections.FXCollections;
  6  import javafx.collections.ObservableList;
  7  import javafx.scene.Scene;
  8  import javafx.scene.control.TableColumn;
  9  import javafx.scene.control.TableView;
 10  import javafx.scene.control.cell.PropertyValueFactory;
 11  import javafx.scene.layout.Pane;
 12  import javafx.stage.Stage;
 13  
 14  public class TableViewDemo extends Application {
 15    @Override // Override the start method in the Application class
 16    public void start(Stage primaryStage) {
 17      TableView<Country> tableView = new TableView<>();
 18      ObservableList<Country> data =
 19        FXCollections.observableArrayList(
 20          new Country("USA", "Washington DC", 280, true),
 21          new Country("Canada", "Ottawa", 32, true),
 22          new Country("United Kingdom", "London", 60, true),
 23          new Country("Germany", "Berlin", 83, true),
 24          new Country("France", "Paris", 60, true));
 25      tableView.setItems(data);
 26      
 27      TableColumn countryColumn = new TableColumn("Country");
 28      countryColumn.setMinWidth(100);
 29      countryColumn.setCellValueFactory(
 30        new PropertyValueFactory<Country, String>("country"));
 31  
 32      TableColumn capitalColumn = new TableColumn("Capital");
 33      capitalColumn.setMinWidth(100);
 34      capitalColumn.setCellValueFactory(
 35        new PropertyValueFactory<Country, String>("capital"));
 36  
 37      TableColumn populationColumn = 
 38        new TableColumn("Population (million)");
 39      populationColumn.setMinWidth(200);
 40      populationColumn.setCellValueFactory(
 41        new PropertyValueFactory<Country, Double>("population"));
 42  
 43      TableColumn democraticColumn = 
 44        new TableColumn("Is Democratic?");
 45      democraticColumn.setMinWidth(200);
 46      democraticColumn.setCellValueFactory(
 47        new PropertyValueFactory<Country, Boolean>("democratic"));
 48  
 49      tableView.getColumns().addAll(countryColumn, capitalColumn,
 50        populationColumn, democraticColumn);
 51  
 52      Pane pane = new Pane();
 53      pane.getChildren().add(tableView);
 54      Scene scene = new Scene(pane, 300, 250);  
 55      primaryStage.setTitle("TableViewDemo"); // Set the window title
 56      primaryStage.setScene(scene); // Place the scene in the window
 57      primaryStage.show(); // Display the window
 58    }
 59  
 60    public static class Country {
 61      private final SimpleStringProperty country;
 62      private final SimpleStringProperty capital;
 63      private final SimpleDoubleProperty population;
 64      private final SimpleBooleanProperty democratic;
 65  
 66      private Country(String country, String capital,
 67          double population, boolean democratic) {
 68        this.country = new SimpleStringProperty(country);
 69        this.capital = new SimpleStringProperty(capital);
 70        this.population = new SimpleDoubleProperty(population);
 71        this.democratic = new SimpleBooleanProperty(democratic);
 72      }
 73  
 74      public String getCountry() {
 75        return country.get();
 76      }
 77  
 78      public void setCountry(String country) {
 79        this.country.set(country);
 80      }
 81  
 82      public String getCapital() {
 83        return capital.get();
 84      }
 85  
 86      public void setCapital(String capital) {
 87        this.capital.set(capital);
 88      }
 89  
 90      public double getPopulation() {
 91        return population.get();
 92      }
 93  
 94      public void setPopulation(double population) {
 95        this.population.set(population);
 96      }
 97  
 98      public boolean isDemocratic() {
 99        return democratic.get();
100      }
101  
102      public void setDemocratic(boolean democratic) {
103        this.democratic.set(democratic);
104      }
105    }
106  
107    /**
108     * The main method is only needed for the IDE with limited
109     * JavaFX support. Not needed for running from the command line.
110     * line.
111     */
112    public static void main(String[] args) {
113      launch(args);
114    }
115  }