JavaFX数据没有出现在TableView中?

时间:2017-11-06 21:16:00

标签: java javafx tableview

似乎我的回调函数措辞正确,每当我尝试将数据添加到ObservableList时,我都有一个print语句,告诉我它已被正确添加。所以我知道数据正在被添加,但它没有显示出来。可能是因为我有不同的套餐吗?或者可能与我用来检索用户输入数据的Alert表达式有关?

这是我想要在tableview中显示的数据的代码:

public class Project
{
private StringProperty name;
private StringProperty date;

public Project(String name, String date)
{
    this.name = new SimpleStringProperty(name);
    this.date = new SimpleStringProperty(date);
}

public String getName()
{
    return name.get();
}
public StringProperty nameProperty()
{
    return name;
}

public void setName(String name)
{
    this.name.set(name);
}

public String getDate()
{
    return date.get();
}

public StringProperty dateProperty()
{
    return date;
}

public void setDate(String date)
{
    this.date.set(date);
}
}

这是我初始化表格的地方:

package sb.FX;

import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import sb.Data.Project;
import sb.ScheduleBuilderApp;

public class ProjectsWindow
{
// Have access to singleton components
ScheduleBuilderApp app;

// Pane & Scene
BorderPane pane;
Scene scene;

// TableView and Columns
TableView<Project> table;
TableColumn<Project, StringProperty> nameColumn;
TableColumn<Project, StringProperty> dateColumn;
VBox tableBox;

// Buttons - Bottom component
Button newButton;
Button removeButton;
Button renameButton;
HBox bottomButtons;

public ProjectsWindow()
{
    initLayout();
    initStyle();
    initControllers();
}

private void initLayout()
{
    // Initialize all buttons
    newButton = new Button("New");
    removeButton = new Button("Remove");
    renameButton = new Button("Rename");

    // Add all buttons in an hbox
    bottomButtons = new HBox();
    bottomButtons.setSpacing(10.0);
    bottomButtons.getChildren().addAll(newButton, removeButton, renameButton);
    bottomButtons.setAlignment(Pos.CENTER);

    // Create the table
    table = new TableView<>();
    nameColumn = new TableColumn("Name");
    dateColumn = new TableColumn("Date Last Edited");
    nameColumn.setCellValueFactory(
            new PropertyValueFactory<>("Name")
    );
    dateColumn.setCellValueFactory(
            new PropertyValueFactory<>("Date")
    );
    table.getColumns().addAll(nameColumn, dateColumn);
    table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

    // Put table in VBox and set spacing
    tableBox = new VBox();
    tableBox.setAlignment(Pos.TOP_CENTER);
    tableBox.getChildren().add(table);

    // Create the pane and set up the panes layout
    pane = new BorderPane();
    pane.setAlignment(bottomButtons, Pos.CENTER);
    pane.setMargin(bottomButtons, new Insets(12, 12, 12, 12));
    pane.setBottom(bottomButtons);
    pane.setMargin(tableBox, new Insets(25, 25, 25, 25));
    pane.setCenter(tableBox);
    scene = new Scene(pane, 450, 450);
}

private void initControllers()
{
    ProjectsController controller = new ProjectsController();

    newButton.setOnAction(e->{
        controller.newButtonEvent();
        table.refresh();
    });
}

private void initStyle()
{}

public Scene getScene()
{
    return scene;
}
}

这是我尝试添加数据的地方:

package sb.FX;

import javafx.scene.control.TextInputDialog;
import sb.Data.Project;
import sb.ScheduleBuilderApp;
import java.time.LocalDate;

import java.time.format.DateTimeFormatter;
import java.util.Optional;

public class ProjectsController
{
    ScheduleBuilderApp app;

public ProjectsController()
{
    // Have access to app
    app = ScheduleBuilderApp.getSingleton();
}

public void newButtonEvent()
{
    // Use built in Java Dialog for text input
    TextInputDialog dialog = new TextInputDialog("name");
    dialog.setTitle("Enter Name");
    dialog.setHeaderText("Please name the Schedule");
    dialog.setContentText("Name of Schedule: ");

    // Obtain result and make a project
    Optional<String> result = dialog.showAndWait();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    LocalDate localDate = LocalDate.now();
    result.ifPresent(name ->
            app.getData().getProjectsData().add(new Project(result.get(), dtf.format(localDate)))
    );

    System.out.println(app.getData().getProjectsData().get(0).getName());
}
}

谢谢!

0 个答案:

没有答案