Javafx TableView不能显示任何内容,但是可以正确显示吗?

时间:2018-12-18 23:02:15

标签: java javafx tableview

我一直在尝试创建一个简单的表,该表由用户可以输入的值组成。但是,尽管我的代码编译没有错误,但似乎我的代码中缺少某些内容。我已经检查了大约两个小时的代码,但仍然看不到任何错误。这里有人可以提供帮助吗?

该按钮也不起作用。

main4类包含main方法。 Main3是一个构造函数。

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 *
 * @author SHAWN
 */
public class Main4 extends Application {

    @Override
    public void start(Stage primaryStage){
        primaryStage.setTitle("999 Emergency Operator System");
        primaryStage.setWidth(450);
        primaryStage.setHeight(550);

        TableView<Main3> table = new TableView<>();
        ObservableList<Main3> data = FXCollections.observableArrayList(
        new Main3("-","-","-"),
        new Main3("-","-","-"));
        table.setItems(data);
        table.setEditable(true);
        Label label = new Label("Operator Reports Pannel");
        label.setFont(new Font("Arial", 20));

        //Creation of Columns here!
        TableColumn firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setMinWidth(200);
            firstNameCol.setCellValueFactory(new PropertyValueFactory<>("fName"));
    TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(25);
            lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lName"));
    TableColumn emailCol = new TableColumn("Email)");
        emailCol.setMinWidth(25);
            emailCol.setCellValueFactory(new PropertyValueFactory<>("mail"));

        //Creation of TextFields
        TextField firstName, lastName, mail;
        firstName = new TextField();
        firstName.setPromptText("First Name");
        firstName.setMinWidth(25);

        lastName = new TextField();
        lastName.setPromptText("Last Name");
        lastName.setMinWidth(25);

        //Time TextFields here
        mail = new TextField();
        mail.setPromptText("Email Address");
        mail.setMinWidth(25);


        Button AddButton = new Button("Add");
        AddButton.setOnAction(e ->{
            data.add(
            new Main3(
            firstName.getText(),
                    lastName.getText(),
                    mail.getText())

            );
        });
        firstName.clear();
        lastName.clear();
        mail.clear();


        HBox hbox1 = new HBox(5);
        hbox1.getChildren().addAll(firstName, lastName, mail, AddButton);

        VBox vbox1 =new VBox(5);
        vbox1.getChildren().addAll(label, table, hbox1);
        vbox1.setPadding(new Insets(10, 0, 0, 10));
        Scene scene = new Scene(new Group());
        ((Group)scene.getRoot()).getChildren().addAll(vbox1);

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


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

}

我有一个错误是在Main3类中,但是我找不到它。

import javafx.beans.property.SimpleStringProperty;

public class Main3 {
    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty email;

    public Main3( String fName, String lName, String mail){

        this.firstName = new SimpleStringProperty(fName);
        this.lastName = new SimpleStringProperty(lName);
        this.email = new SimpleStringProperty(mail);


}
    public String getFirstName(){
        return firstName.get();
    }
    public String getLastName(){
        return lastName.get();
    }
    public String getEmail(){
        return email.get();
    }
    public void setFirstName(String fName){
        firstName.set(fName);
    }
    public void setLastName(String LName){
        lastName.set(LName);
    }
    public void setEmailName(String mail){
        email.set(mail);
    }
}

0 个答案:

没有答案