やさしいJAVA 活用編第6版 テーブルビューの表示

テーブルビューの表示

f:id:Pavilion2021:20220122091226p:plain

設計の考え方が理解できないので自分用メモ

①コントロール(テーブルビュー)の作成

tv = new TableView<RowData>();

②コントロール(列)の設定

(「日付列」と「営業列」の作成)

TableColumn<RowData, String> tc1 = new TableColumn<RowData, String>("日付");

TableColumn<RowData, String> tc2 = new TableColumn<RowData, String>("営業");

(列 tc1 と tc2 をプロパティ "date" と "business"と紐づける)

tc1.setCellValueFactory(new PropertyValueFactory<RowData, String>("date"));

tc2.setCellValueFactory(new PropertyValueFactory<RowData, String>("business"));

データの作成

ObservableList<RowData> ol = FXCollections.observableArrayList();

for(int i=0; i<50; i++){ol.add(new RowData(i));}

列をテーブルビューに設定

tv.getColumns().add(tc1);

tv.getColumns().add(tc2);

データ(ObservableList<RowData> ol)をテーブルビュー(tv)に設定

tv.setItems(ol);

==================================================

データクラスの記述

   //RowDataクラスの設定
   public class RowData
   {
      private final SimpleStringProperty date;
      private final SimpleStringProperty business;

      public RowData(int row)
      {
         DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd");
         LocalDateTime t = LocalDateTime.now();
         LocalDateTime d = t.plusDays(row);

         this.date = new SimpleStringProperty(df.format(d));

         if(d.getDayOfWeek() == DayOfWeek.SUNDAY) this.business = new SimpleStringProperty("休業日です。");
         else this.business = new SimpleStringProperty("営業日です。");
         }

         public StringProperty dateProperty(){return date;}
         public StringProperty businessProperty(){return business;}

   }