Почему дата не отображается в Tableview javafx? - PullRequest
0 голосов
/ 17 декабря 2018

Моя проблема в том, что когда я компилирую код, столбец даты пуст.Я не мог найти решение ни на одном сайте.Код чуть ниже.Я думаю, что для этой программы должен отсутствовать файл fxml.Я использую компилятор Eclipse Photon Release (4.8.0)

import java.time.LocalDate;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;

import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewTest extends Application {

    private TableView<Conteudo> table = new TableView<Conteudo>();
    private final ObservableList<Conteudo> data = FXCollections.observableArrayList(new Conteudo(LocalDate.now(),"2","teste"),
            new Conteudo(LocalDate.of(2018, 1, 2), "3","teste2"));

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(450);
        stage.setHeight(500);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        table.setEditable(true);

        TableColumn<Conteudo, String> dataCol = new TableColumn<Conteudo, String>("DATA");
        dataCol.setMinWidth(100);
        dataCol.setCellValueFactory(new PropertyValueFactory<>("data"));

        TableColumn aulaCol = new TableColumn("AULAS");
        aulaCol.setMinWidth(100);
        aulaCol.setCellValueFactory(
                new PropertyValueFactory<Conteudo, String>("aula"));

        TableColumn conteudoCol = new TableColumn("CONTEUDOS");
        conteudoCol.setMinWidth(200);
        conteudoCol.setCellValueFactory(
                new PropertyValueFactory<Conteudo, String>("conteudo"));

        table.setItems(data);
        table.getColumns().addAll(dataCol, aulaCol, conteudoCol);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }
}

Ниже приведен код, управляющий данными:

import java.time.LocalDate;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Conteudo {

    private final ObjectProperty<LocalDate> data = new SimpleObjectProperty<>(this, "data",null);
    private final StringProperty aula = new SimpleStringProperty(this, "aula",null);
    private final StringProperty conteudo = new SimpleStringProperty(this, "conteudo", null);

    public Conteudo() { 
        this(null, null, null); // chama outro construtor
    }

    public Conteudo(LocalDate dt, String au, String cont) {
        this.data.set(dt);
        this.aula.set(au);
        this.conteudo.set(cont);
    }

    public void setDate(LocalDate dt) {
        data.set(dt);
    }
    public LocalDate getDate() {
        return data.get();
    }

    public final ObjectProperty<LocalDate> dateProperty() {
        return data;
    }

    public void setAula(String au) {
        this.aula.set(au);
    }
    public String getAula() {
        return aula.get();
    }
    public void setConteudo(String cont) {
        this.conteudo.set(cont);
    }
    public String getConteudo() {
        return conteudo.get();
    }

}
...