JavaFX TableView data not visible with CellValueFactory


Question

I'm working on a finance control type application for my parents in Java using JavaFX, but I'm having a problem with the GUI. I've searched Google and the code for a problem, but I can't seem to find anything wrong.

This is the link I'm using as a guideline: http://docs.oracle.com/javafx/2/ui_controls/table-view.htm

I'm starting off with a Calender using TableView. By the way, is there any other way I can make a calender with JavaFX?

Anyways, here is the code:

package Finance;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
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;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main extends Application {
    Calendar calendar = new GregorianCalendar();
    private TableView<Week> table = new TableView<Week>();

    final ObservableList<Week> data = FXCollections.observableArrayList(
            new Week("1", "2", "3", "4", "5", "6", "7")
    );

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(new Group());
        primaryStage.setTitle("Finance Control");
        primaryStage.setWidth(1000);
        primaryStage.setHeight(700);

        Label month = new Label(getMonthForInt(calendar.get(Calendar.MONTH)));
        month.setFont(new Font("Arial", 30));

        table.setEditable(false);
        table.setPrefHeight(500);
        table.setPrefWidth(882);

        TableColumn sun = new TableColumn("Sunday");
        TableColumn mon = new TableColumn("Monday");
        TableColumn tue = new TableColumn("Tuesday");
        TableColumn wed = new TableColumn("Wednesday");
        TableColumn thu = new TableColumn("Thursday");
        TableColumn fri = new TableColumn("Friday");
        TableColumn sat = new TableColumn("Saturday");

        sun.setCellValueFactory(
                new PropertyValueFactory<Week, String>("a")
        );

        mon.setCellValueFactory(
                new PropertyValueFactory<Week, String>("b")
        );

        tue.setCellValueFactory(
                new PropertyValueFactory<Week, String>("c")
        );

        wed.setCellValueFactory(
                new PropertyValueFactory<Week, String>("d")
        );

        thu.setCellValueFactory(
                new PropertyValueFactory<Week, String>("e")
        );

        fri.setCellValueFactory(
                new PropertyValueFactory<Week, String>("f")
        );

        sat.setCellValueFactory(
                new PropertyValueFactory<Week, String>("g")
        );

        TableColumn[] days = {sun, mon, tue, wed, thu, fri, sat};
        for (TableColumn day: days) {
            day.setPrefWidth(table.getPrefWidth()/7);
        }

        table.setItems(data);
        table.getColumns().addAll(sun, mon, tue, wed, thu, fri, sat);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.setAlignment(Pos.CENTER);
        vbox.getChildren().addAll(month, table);
        ((Group) scene.getRoot()).getChildren().addAll(vbox);

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

    String getMonthForInt(int num) {
        String month = "wrong";
        DateFormatSymbols dfs = new DateFormatSymbols();
        String[] months = dfs.getMonths();
        if (num >= 0 && num <= 11 ) {
            month = months[num];
        }
        return month;
    }

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

    public static class Week {
        String a;
        String b;
        String c;
        String d;
        String e;
        String f;
        String g;

        public Week(String a, String b, String c, String d, String e, String f, String g) {
            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
            this.e = e;
            this.f = f;
            this.g = g;
        }
    }
}

This is what happens when I run it. I would expect it to populate with the values I gave the Constructor of the Week object, but it isn't - it just shows empty cells.

http://i.imgur.com/ZrhPIDe.png

I'm using IDEA IDE if that matters... I'd be grateful for any help with this or any comments. I'll probably be using scenebuilder at some point, but I wanted to get a feel for the actual code first before going to the GUI builder.

1
0
1/20/2014 4:01:23 AM

Accepted Answer

Ahh, it appears I've solved it. The PropertyValueFactory class uses the methods in the week class to get the values - I simply needed to add some getters for all of the fields.

0
1/20/2014 4:23:06 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon