Как в колонке TableView отображать значение последнего элемента ListProperty (javaFX)

Рейтинг: 0Ответов: 1Опубликовано: 08.05.2023

уважаемые коллеги! Подскажите, пожалуйста, каким образом в одной ячейке TableView отобразить значение элемента ListProperty. Детали: У меня есть класс SomeWork, который содержит переменные следующих типов: int, String,double,LocalData. Есть объект типа Plant, который содержит несколько List -SomeWork-. В TableView добавляю List-Plant-, первые две колонки (double и String) отображаются корректно, но к колонкам, в которых должен быть List-SomeWork- добавить Property не получается, соответственно они ничего не отображают. Вот такую форму нужно создать

Прикладываю код

public class SomeWork {
    private int volume;
    private double weight;
    private String substance;
    private String date;
    DateTimeFormatter formatters = DateTimeFormatter.ofPattern("dd.MM.uuuu");

    //полив
    public SomeWork(){
        LocalDate dataNow = LocalDate.now();
        this.date = dataNow.format(formatters);
    }
    public SomeWork (int volume){
        this.volume=volume;
        LocalDate dataNow = LocalDate.now();
        this.date = dataNow.format(formatters);
    }
    //седерация
    public SomeWork (String substance){
        this.substance = substance;
        LocalDate dataNow = LocalDate.now();
        this.date = dataNow.format(formatters);
    }
    //минерализация, раскисление
    public SomeWork (double weight,String substance) {
        this.weight = weight;
        this.substance = substance;
        LocalDate dataNow = LocalDate.now();
        this.date = dataNow.format(formatters);
    }
        //урожай
    public SomeWork (double weight){
            this.weight=weight;
            LocalDate dataNow = LocalDate.now();
            this.date = dataNow.format(formatters);
    }

    public int getVolume() {
        return volume;
    }
    public double getWeight() {return weight;}
    public String getSubstance() {
        return substance;
    }
    public String getDate() {
        return date;
    }
    public void setVolume(int volume) {
        this.volume = volume;
    }
    public void setWeight(double weight) {this.weight = weight;}
    public void setSubstance(String substance) {
        this.substance = substance;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public DateTimeFormatter getFormatters() {
        return formatters;
    }
}




public class Plant {

    private final DoubleProperty numberRidge;
    private final StringProperty name;
    private final ListProperty<SomeWork> watering;
  private final ListProperty<SomeWork> mineralization;
    private final ListProperty<SomeWork> mulching;
    private final ListProperty<SomeWork> deoxidation;
    private final ListProperty<SomeWork> sedation;
    private final ListProperty<SomeWork> harvesting;


    public Plant(double numberRidge,String name){
        this.numberRidge=new SimpleDoubleProperty(numberRidge);
        this.name= new SimpleStringProperty(name);
        ObservableList<SomeWork> wateringList = FXCollections.observableArrayList( new SomeWork(0) );
        this.watering = new SimpleListProperty<>(wateringList);
        ObservableList<SomeWork> mineralizationList = FXCollections.observableArrayList( new SomeWork(0,"добавьте минерализацию") );
       this.mineralization = new SimpleListProperty<>(mineralizationList);
        ObservableList<SomeWork> mulchingList = FXCollections.observableArrayList( new SomeWork());
        this.mulching = new SimpleListProperty<>(mulchingList);
        ObservableList<SomeWork> deoxidationList = FXCollections.observableArrayList( new SomeWork(0,"добавьте раскисление") );
        this.deoxidation = new SimpleListProperty<>(deoxidationList);
        ObservableList<SomeWork> sedationList = FXCollections.observableArrayList( new SomeWork("добавьте сидерацию") );
        this.sedation = new SimpleListProperty<>(sedationList);
        ObservableList<SomeWork> harvestingList = FXCollections.observableArrayList( new SomeWork(0) );
        this.harvesting = new SimpleListProperty<>(harvestingList);
    }

    public DoubleProperty NumberRidgeProperty() {
        return numberRidge;
    }
    public double getNumberRidge() {
        return numberRidge.get();
    }
    public void setNumberRidge(double numberRidge) {
        this.numberRidge.set(numberRidge);
    }

    public StringProperty NameProperty() {return name;}
    public String getName() {
        return name.get();
    }
    public void setName(String name) {this.name.set(name);}

    public ListProperty<SomeWork> WateringProperty() {return watering;}
    public ObservableList<SomeWork> getWatering() {return watering.get();}
    public void setWatering(ObservableList<SomeWork> watering) {
        this.watering.set(watering);
    }

    public ListProperty<SomeWork> MineralizationProperty() {
        return mineralization;
    }
    public ObservableList<SomeWork> getMineralization() {
        return mineralization.get();
    }
    public void setMineralization(ObservableList<SomeWork> mineralization) {
        this.mineralization.set(mineralization);
    }

    public ListProperty<SomeWork> MulchingProperty() {return mulching;}
    public ObservableList<SomeWork> getMulching() {return mulching.get();}
    public void setMulching(ObservableList<SomeWork> mulching) {
        this.mulching.set(mulching);
    }

    public ListProperty<SomeWork> DeoxidationProperty() {return deoxidation;}
    public ObservableList<SomeWork> getDeoxidation() {return deoxidation.get();}
    public void setDeoxidation(ObservableList<SomeWork> deoxidation) {
        this.deoxidation.set(deoxidation);
    }

    public ListProperty<SomeWork> SedationProperty() {return sedation;}
    public ObservableList<SomeWork> getSedation() {return sedation.get();}
    public void setSedation(ObservableList<SomeWork> sedation) {
        this.sedation.set(sedation);
    }

    public ListProperty<SomeWork> HarvestingProperty() {return harvesting;}
    public ObservableList<SomeWork> getHarvesting() {return harvesting.get();}
    public void setHarvesting(ObservableList<SomeWork> harvesting) {
        this.harvesting.set(harvesting);
    }
}

// Привязываю колонки к property, метод asObject() работает только для int,doubl, для ListProperty не существует

 private void initialize() {

        col2023_1.setCellValueFactory(cellData -> cellData.getValue().NumberRidgeProperty().asObject());
      col2023_2.setCellValueFactory(cellData -> cellData.getValue().NameProperty());
       col2023_3.setCellValueFactory(cellData -> cellData.getValue().WateringProperty());
    }

Буду благодарна за любую помощь!

Ответы

▲ 0

Может быть кому-нибудь будет полезен способ, которым я воспользовалась.

Добавила в конструктор Plant переменные типа StringProperty, значения которых соответствует последнему элементу List. И уже к этим переменным привязывала значение колонок.

this.wateringLastElement=new SimpleStringProperty("не поливали");     
public StringProperty wateringLastElementProperty() {
        return wateringLastElement; }
    public String getWateringLastElement() {return wateringLastElement.get();}
    public void setWateringLastElement() {this.wateringLastElement.set(String.valueOf(wateringList.get(wateringList.size()-1).getVolume()+" л.   " + wateringList.get(wateringList.size()-1).getDate()));}
 col2025_3.setCellValueFactory(cellData -> cellData.getValue().wateringLastElementProperty());