JavaFX tableview无法正常显示

时间:2015-03-21 09:58:45

标签: java javafx tableview

我有一个带有两个tableview的场景的JavaFX应用程序,第一个正常工作,第二个没有。

第二个tableview只显示它应该在5列中的2个内容,而第二列中的内容应该实际上在最后一列上。我以前曾经使用过javaFX几次,这是我第一次看到这样的东西,我修改了我的代码2到3次,寻找可能是错误或错误的东西,但我找不到它。

这是tableview显示的内容。

enter image description here

这是它包含的对象类的代码。

public class Producte {

    private int codi;
    private String nom;
    private String descripcio;
    private float preu;
    private int codiFabricant;

    public Producte(String nom, String descripcio, float preu,
            int codiFabricant) {
        this.nom = nom;
        this.descripcio = descripcio;
        this.preu = preu;
        this.codiFabricant = codiFabricant;
    }

    public Producte(int codi, String nom, String descripcio, float preu,
            int codiFabricant) {
        this.codi = codi;
        this.nom = nom;
        this.descripcio = descripcio;
        this.preu = preu;
        this.codiFabricant = codiFabricant;
    }

    public int getCodi() {
        return codi;
    }

    public void setCodi(int codi) {
        this.codi = codi;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getDescripcio() {
        return descripcio;
    }

    public void setDescripcio(String descripcio) {
        this.descripcio = descripcio;
    }

    public float getPreu() {
        return preu;
    }

    public void setPreu(float preu) {
        this.preu = preu;
    }

    public int getCodiFabricant() {
        return codiFabricant;
    }

    public void setCodiFabricant(int codiFabricant) {
        this.codiFabricant = codiFabricant;
    }

    @Override
    public String toString() {
        return "Producte [codi=" + codi + ", nom=" + nom + ", descripcio="
                + descripcio + ", preu=" + preu + ", codiFabricant="
                + codiFabricant + "]";
    }

}

这是加载场景的方法中的代码。 第一个tableview是dirTable,第二个(不能正常工作的是prodTable)。在此先感谢您的帮助:/

static Scene directorScene() {
        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setVgap(25);
        gridPane.setHgap(25);

        director = DirectorFunctions.director;

        final ObservableList<Director> dirList = FXCollections.observableArrayList();
        dirList.add(director);

        TableView<Director> dirTable = new TableView<>();
        dirTable.setItems(dirList);

        TableColumn<Director, String> fabCol = new TableColumn<>("Fabricant");
        TableColumn<Director, String> dniCol = new TableColumn<>("DNI");
        TableColumn<Director, String> nomCol = new TableColumn<>("Nom");
        TableColumn<Director, String> mailCol = new TableColumn<>("Mail");
        TableColumn<Director, String> passCol = new TableColumn<>("Pass");

        fabCol.setCellValueFactory(new PropertyValueFactory<Director,String>("Nom"));
        dniCol.setCellValueFactory(new PropertyValueFactory<Director,String>("DniDirector"));
        nomCol.setCellValueFactory(new PropertyValueFactory<Director,String>("NomDirector"));
        mailCol.setCellValueFactory(new PropertyValueFactory<Director,String>("MailDirector"));
        passCol.setCellValueFactory(new PropertyValueFactory<Director,String>("PasswordDirector"));

        dirTable.getColumns().setAll(fabCol, dniCol, nomCol, mailCol, passCol);
        dirTable.setMinWidth(450);
        dirTable.setMaxHeight(52);
        dirTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        gridPane.add(dirTable, 0, 0);

        Button modificarDirector = new Button("Modificar");
        modificarDirector.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub

            }
        });
        gridPane.add(modificarDirector, 1, 0);

        final ObservableList<Producte> prodList = FXCollections.observableArrayList();
        for (Producte p:DirectorFunctions.productes.values()) {
            prodList.add(p);
        }

        TableView<Producte> prodTable = new TableView<>();
        prodTable.setItems(prodList);

        TableColumn<Producte, String> codiPCol = new TableColumn<>("Codi");
        TableColumn<Producte, String> nomPCol = new TableColumn<>("Nom");
        TableColumn<Producte, String> descPCol = new TableColumn<>("Descripció");
        TableColumn<Producte, String> preuPCol = new TableColumn<>("Preu");
        TableColumn<Producte, String> fabPCol = new TableColumn<>("Fabricant");

        codiPCol.setCellValueFactory(new PropertyValueFactory<Producte, String>("Codi"));
        nomPCol.setCellValueFactory(new PropertyValueFactory<Producte, String>("Nom"));
        nomPCol.setCellValueFactory(new PropertyValueFactory<Producte, String>("Descripcio"));
        nomPCol.setCellValueFactory(new PropertyValueFactory<Producte, String>("Preu"));
        nomPCol.setCellValueFactory(new PropertyValueFactory<Producte, String>("CodiFabricant"));

        prodTable.getColumns().setAll(codiPCol, nomPCol, descPCol, preuPCol, fabPCol);
        prodTable.setMinWidth(450);
        prodTable.setMaxHeight(200);
        prodTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        gridPane.add(prodTable, 0, 1);

        Button nouButton = new Button("Nou");
        nouButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub

            }
        });
        nouButton.setMinWidth(75);

        Button modButton = new Button("Modificar");
        modButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub

            }
        });
        modButton.setMinWidth(75);

        Button delButton = new Button("Borrar");
        delButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub

            }
        });
        delButton.setMinWidth(75);

        Button backButton = new Button("Tornar");
        backButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                // TODO Auto-generated method stub

            }
        });
        backButton.setMinWidth(75);
        gridPane.add(backButton, 1, 2);

        VBox vbox = new VBox(25);
        vbox.getChildren().addAll(nouButton, modButton, delButton);
        gridPane.add(vbox, 1, 1);

        Scene scene = new Scene(gridPane, 640, 480, Color.web("eee"));
        return scene;
    }

这是任何想要看到它的人的导演课程

public class Director {

    private int codi;
    private String nom;
    private String dniDirector;
    private String nomDirector;
    private String mailDirector;
    private String passwordDirector;

    public Director(int codi, String nom, String dniDirector,
            String nomDirector, String mailDirector, String passwordDirector) {
        this.codi = codi;
        this.nom = nom;
        this.dniDirector = dniDirector;
        this.nomDirector = nomDirector;
        this.mailDirector = mailDirector;
        this.passwordDirector = passwordDirector;
    }

    public int getCodi() {
        return codi;
    }

    public void setCodi(int codi) {
        this.codi = codi;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getDniDirector() {
        return dniDirector;
    }

    public void setDniDirector(String dniDirector) {
        this.dniDirector = dniDirector;
    }

    public String getNomDirector() {
        return nomDirector;
    }

    public void setNomDirector(String nomDirector) {
        this.nomDirector = nomDirector;
    }

    public String getMailDirector() {
        return mailDirector;
    }

    public void setMailDirector(String mailDirector) {
        this.mailDirector = mailDirector;
    }

    public String getPasswordDirector() {
        return passwordDirector;
    }

    public void setPasswordDirector(String passwordDirector) {
        this.passwordDirector = passwordDirector;
    }

    @Override
    public String toString() {
        return "Fabricant [codi=" + codi + ", nom=" + nom + ", dniDirector="
                + dniDirector + ", nomDirector=" + nomDirector
                + ", mailDirector=" + mailDirector + ", passwordDirector="
                + passwordDirector + "]";
    }

}

2 个答案:

答案 0 :(得分:1)

我建议您将JavaFX属性用于所有模型。您可以在here中找到示例。

PropertyValueFactory<S,T>T中查找S类型的属性,其名称与您在构造函数中输入的名称相同。

例如,在具有StringProperty字段Product的模型类name中,

new PropertyValueFactory<Product, String>("name")

将寻找:

nameProperty()

如果它找不到它,它会进一步查找getName()并在返回之前将其包裹在ReadOnlyObjectWrapper中。

来自文档,

  

如果不存在与此模式匹配的方法,则存在漏洞   支持尝试调用get()或is()(即   是,上面示例中的getFirstName()或isFirstName())。如果一个方法   如果匹配此模式,则此方法返回的值为   包装在ReadOnlyObjectWrapper中并返回到TableCell

您的示例中的问题是您的表列有一个cellValueFactory,表示

nomPCol.setCellValueFactory(new PropertyValueFactory<Producte, String>("Preu"));

但是您的getter方法返回的是float而不是String

public float getPreu() {
    return preu;
}

答案 1 :(得分:0)

我已经发现了问题,我使用复制+粘贴来复制代码行,我忘了更改表的列上的名称。我是个白痴-__-。

抱歉浪费你的时间......

顺便问一下,有没有办法在没有问mods之类的情况下关闭一个帖子/问题?