从LocalDate到类实例化的日期转换不起作用

时间:2017-11-19 20:35:39

标签: javafx datepicker tableview tablecolumn localdate

我正在开展的这个新项目,我们应该建立一个GUI应用程序,作为囚犯的警长预订系统。我觉得我在正确的轨道上,但我开始遇到一些障碍。

我在使用datePickers时遇到了一些麻烦,并将日期传递给了一个" Inmate"(类)实例化。没有编译错误,但是当我尝试从用户输入添加囚犯时发生了一些错误。它不会让我在没有抛出nullpointerexception的情况下首先选择arrestDate,但如果我从发布日期开始,那么我不会遇到同样的问题,我不知道为什么。我使用tableView显示所有值,因为它是我找到的最简单的排序方式。我认为错误来自于尝试将LocalDate转换为Date然后将其传递给实例化。我试图避免使用数据库,因为我还没有接受过关于如何使用java的培训。

还有一个奇怪的错误,我的一个专栏没有显示它应该是的数据,我似乎无法找到我做错的事情。它与inmateIDCol。它不会显示我预设的任何ID。

我也不明白为什么不能自己编辑细胞。

我仍计划对tableview和访问者日志实施过滤器。但我似乎无法在不添加新窗口的情况下围绕如何使用访问者日志tableview更改tableview。只是让一个消失,下一个弹出也是完美的。我想添加一个按钮,说明查看访问者的日志,然后添加"添加"和#34;完成"按钮到访客日志。完成按钮将返回原始囚犯日志。

这是我目前无法解决的主要问题。如果你有任何提示,想法,简单的修复,我可以看到的任何可能有助于它将非常感谢!

public class Final extends Application {
String s;
Format formatter = new SimpleDateFormat("dd-MMM-yy");
Date arrestDate;
Date courtDate;
Date releaseDate;
int IDNum;


    //build inmates
    ObservableList<Inmate> inmateList;
@Override
public void start(Stage primaryStage) {
    //build inmates
    buildInmates();
    //create pane

    TableView tableView = new TableView();//ObservableList<Inmate> inmateList);
    //tableView.setItems(inmateList);
    tableView.setEditable(true);
    //Title
    Label label = new Label("Sheriff Booking System");
    label.setFont(new Font("Arial", 40));
    //set columns
    TableColumn firstNameCol = new TableColumn("First Name");
    firstNameCol.setCellValueFactory(
        new PropertyValueFactory<Inmate,String>("fName")
    );
    firstNameCol.setEditable(true);
    firstNameCol.setMinWidth(150);

    TableColumn lastNameCol = new TableColumn("Last Name");
    lastNameCol.setCellValueFactory(
        new PropertyValueFactory<Inmate,String>("lName")
    );
    lastNameCol.setEditable(true);
    lastNameCol.setMinWidth(175);

    TableColumn inmateIDCol = new TableColumn("Inmate ID");
    inmateIDCol.setCellValueFactory(
        new PropertyValueFactory<Inmate,String>("IDNumber")
    );
    inmateIDCol.setEditable(true);
    inmateIDCol.setMinWidth(100);

    TableColumn cellBlockCol = new TableColumn("Cell Block");
    cellBlockCol.setCellValueFactory(
        new PropertyValueFactory<Inmate,String>("cellBlock")
    );
    cellBlockCol.setEditable(true);
    cellBlockCol.setMinWidth(100);

    TableColumn heightCol = new TableColumn("Height");
    heightCol.setCellValueFactory(
        new PropertyValueFactory<Inmate,String>("height")
    );
    heightCol.setEditable(true);
    heightCol.setMinWidth(100);

    TableColumn weightCol = new TableColumn("Weight");
    weightCol.setCellValueFactory(
        new PropertyValueFactory<Inmate,String>("weight")
    );
    weightCol.setEditable(true);
    weightCol.setMinWidth(100);

    TableColumn ethnicityCol = new TableColumn("Ethnicity");
    ethnicityCol.setCellValueFactory(
        new PropertyValueFactory<Inmate,String>("ethnicity")
    );
    ethnicityCol.setEditable(true);
    ethnicityCol.setMinWidth(100);

    TableColumn arrestDateCol = new TableColumn("Arrest Date(s)");
    arrestDateCol.setCellValueFactory(
        new PropertyValueFactory<Inmate,String>("arrestDate")
    );
    arrestDateCol.setMinWidth(150);

    TableColumn courtDateCol = new TableColumn("Court Date(s)");
    courtDateCol.setCellValueFactory(
        new PropertyValueFactory<Inmate,String>("courtDate")
    );
    courtDateCol.setMinWidth(150);

    TableColumn releaseDateCol = new TableColumn("Release Date(s)");
    releaseDateCol.setCellValueFactory(
        new PropertyValueFactory<Inmate,String>("releaseDate")
    );        
    releaseDateCol.setMinWidth(150);
    //add values to tableview
    tableView.getColumns().addAll(inmateIDCol, firstNameCol, lastNameCol, cellBlockCol, heightCol, weightCol, ethnicityCol, arrestDateCol, courtDateCol, releaseDateCol);

    //textfields to add a new inmate
    HBox hb1 = new HBox();
    hb1.setPadding(new Insets (10));
    hb1.setSpacing(20);
    HBox hb2 = new HBox();
    hb2.setSpacing(20);
    hb2.setPadding(new Insets (10));
    TextField addFName = new TextField();
    addFName.setPromptText("First Name");
    addFName.setMaxWidth(200);

    TextField addLName = new TextField();
    addLName.setPromptText("Last Name");
    addLName.setMaxWidth(200);

    TextField addID = new TextField();
    addID.setPromptText("ID Number");
    addID.setMaxWidth(150);

    TextField addCellBlock = new TextField();
    addCellBlock.setPromptText("Cell Block");
    addCellBlock.setMaxWidth(150);

    TextField addHeight = new TextField();
    addHeight.setPromptText("Height");
    addHeight.setMaxWidth(175);

    TextField addWeight = new TextField();
    addWeight.setPromptText("Weight");
    addWeight.setMaxWidth(175);

    TextField addEthnicity = new TextField();
    addEthnicity.setPromptText("Ethinicty");
    addEthnicity.setMaxWidth(175);

    //datePickers for all date adding
    DatePicker addArrestDate = new DatePicker();
    addArrestDate.setOnAction(e -> {
        LocalDate date = addArrestDate.getValue();
        arrestDate = java.sql.Date.valueOf(date);
        //System.out.println("Selected date: " + arrestDate);
    });

    DatePicker addCourtDate = new DatePicker();
    addArrestDate.setOnAction(e -> {
        LocalDate date = addCourtDate.getValue();
        courtDate = java.sql.Date.valueOf(date);
        //System.out.println("Selected date: " + courtDate);
    });

    DatePicker addReleaseDate = new DatePicker();
    addArrestDate.setOnAction(e -> {
        LocalDate date = addReleaseDate.getValue();
        releaseDate = java.sql.Date.valueOf(date);
        //System.out.println("Selected date: " + releaseDate);
    });
    //button to add new inmate
    Button addButton = new Button("ADD NEW INMATE");
    addButton.setOnAction(e -> {
        IDNum = getInt(addID.getText());
        inmateList.add(new Inmate(
                addFName.getText(),
                addLName.getText(),
                addID.getText(),
                addCellBlock.getText(),
                addHeight.getText(),
                addWeight.getText(),
                addEthnicity.getText(),
                arrestDate,
                courtDate,
                releaseDate));
        addFName.clear();
        addLName.clear();
        addID.clear();
        addCellBlock.clear();
        addHeight.clear();
        addWeight.clear();
        addEthnicity.clear();
        tableView.refresh();
    });
    //labels for datepickers
    Label arrDate = new Label("Arrest Date:");
    arrDate.setFont(new Font("Arial", 18));
    Label courtDate = new Label("Court Date:");
    courtDate.setFont(new Font("Arial", 18));
    Label relDate = new Label("Release Date:");
    relDate.setFont(new Font("Arial", 18));
    Label addNewInmate = new Label("Add New Inmate:");
    addNewInmate.setFont(new Font("Arial", 20));

    tableView.setItems(inmateList);
    //add new inmate textfields and datepickers to view
    hb1.getChildren().addAll( addID, addFName, addLName, addCellBlock, addHeight, addWeight, addEthnicity);
    hb2.getChildren().addAll(arrDate, addArrestDate, courtDate, addCourtDate, relDate, addReleaseDate, addButton);
    VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(10, 0, 0, 10));
    vbox.getChildren().addAll(label, tableView, addNewInmate, hb1, hb2);
    //create date piceker test


    StackPane root = new StackPane();
    root.getChildren().add(vbox);

    Scene scene = new Scene(root, 1400, 700);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

public ObservableList<Inmate> buildInmates()
{
    inmateList = observableArrayList();
    inmateList.add( new Inmate("Joseph", "Levitt", "0001001", "Minimum", "6'1", "205.0", "White",new Date(2014, 14,7), new Date(2014, 30, 6), new Date(2002, 30, 9)));
    inmateList.add( new Inmate("Steve", "Smith", "0001002", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Jordan", "Jones", "0001003", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Samuel", "Adams", "0001004", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Billy", "Joel", "0001006", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Jose", "Cabron", "0001008", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Clark", "Kent", "0001010", "Isolation", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Peter", "Parker", "0001012", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Darrel", "Belk", "0001013", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Shaun", "Stanberry", "0002001", "Maximum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Kemar", "Adjiboue", "0002002", "Maximum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Paul", "Blart", "0002003", "Maximum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Ted", "Bundy", "0002006", "Maximum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Jeffrey", "Jones", "0002004", "Maximum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Geoffrey", "Pittman", "0002009", "Maximum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Michael", "Kersch", "0001021", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Parker", "Smith", "0001022", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Craig", "Robinson", "0001023", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Nigel", "Thornberry", "0001024", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Miguel", "Hinehosa", "0001031", "Minimum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("Charles", "Darwin", "0002015", "Isolation", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));
    inmateList.add( new Inmate("William", "Wundt", "0002007", "Maximum", "6'1", "205.0", "White", new Date( 2014, 14,7), new Date( 2014, 30, 6), new Date( 2002, 30, 9)));


    return null;
}

这是犯人类:

public class Inmate {

//variables
SimpleStringProperty fName;
SimpleStringProperty lName;
SimpleStringProperty cellBlock;
SimpleStringProperty IDNumber;
SimpleStringProperty height; //initialized with ("#'#")
SimpleStringProperty weight ;
SimpleStringProperty ethnicity;
Date arrestDate;
Date courtDate;
Date releaseDate;
//String visitors;
String s;
Format formatter = new SimpleDateFormat("dd-MMM-yy");


public Inmate()
{
    //Default Constructor
    fName = new SimpleStringProperty("Jon");
    lName = new SimpleStringProperty("Doe");
    cellBlock = new SimpleStringProperty("Minimum Security");
    IDNumber = new SimpleStringProperty("00000001");
    height = null;
    weight = new SimpleStringProperty("0.0");
    ethnicity = new SimpleStringProperty("Unknown");
    arrestDate = new Date();
    courtDate = new Date();
    releaseDate = new Date();

}
//minimal constructor
public Inmate(String fName, String lName, String IDNumber, String cellBlock)
{
    this.fName = new SimpleStringProperty(fName);
    this.lName = new SimpleStringProperty(lName);
    this.cellBlock = new SimpleStringProperty(cellBlock);
    this.IDNumber = new SimpleStringProperty(IDNumber);
    height = null;
    weight =  new SimpleStringProperty("0.0");
    ethnicity = new SimpleStringProperty("Unknown");
    arrestDate = new Date();
    courtDate = new Date();
    releaseDate = new Date();
}
//full constructor
public Inmate(String fName, String lName, String IDNumber, String cellBlock, String height, String weight, String ethnicity, Date arrestDate, Date courtDate, Date releaseDate)
{
    this.fName = new SimpleStringProperty(fName);
    this.lName = new SimpleStringProperty(lName);
    this.cellBlock = new SimpleStringProperty(cellBlock);
    this.IDNumber = new SimpleStringProperty(IDNumber);
    this.height = new SimpleStringProperty(height);
    this.weight = new SimpleStringProperty(weight);
    this.ethnicity = new SimpleStringProperty(ethnicity);
    this.arrestDate = arrestDate;
    this.courtDate = courtDate;
    this.releaseDate = releaseDate;
}

//set and get first name
public void setFName(String name)
{
    this.fName.set(name);
}
public String getFName()
{
    return fName.get();
}

//get and set last name
public void setLname(String name)
{
    this.lName.set(name);
}
public String getLName()
{
    return lName.get();
}

//set and get cell block
public void setCellBlock(String cell)
{
    this.cellBlock.set(cell);
}
public String getCellBlock()
{
    return cellBlock.get();
}

//set and get ID number
public void setIDNumber(String num)
{
    this.IDNumber.set(num);
}
public String getID()
{
    return IDNumber.get();
}

//set and get height
public void setHeight(String height)
{
    this.height.set( height);
}
public String getHeight()
{
    return height.get();
}

//set and get weight
public void setWeight(String weight)
{
    this.weight.set(weight);
}
public String getWeight()
{
    return weight.get();
}

//set and get ethnicity
public void setEthnicity(String race)
{
    this.ethnicity.set(race);
}
public String getEthnicity()
{
    return ethnicity.get();
}


//set and get arrest date
public void setArrestDate (Date date)
{
    arrestDate = date;
}
public String getArrestDate()
{
    return formatter.format(arrestDate);
}

//set adn get court date
public void setCourtDate (Date date)
{
    courtDate = date;
}
public String getCourtDate()
{
    return formatter.format(courtDate);
}

//set and get release date
public void setReleaseDate(Date date)
{
    releaseDate = date;
}
public String getReleaseDate()
{
    return formatter.format(releaseDate);
}

}

0 个答案:

没有答案