无法在javafx中填充表格

时间:2019-04-29 23:53:24

标签: java javafx javafx-2

我正在开发一个小型应用程序,我需要在表中列出所有已注册的客户端,但似乎无法做到

我还想知道是否有任何方法可以使用诸如cliente.getEnderecoPostal.toString之类的方法填充其他列

我尝试了在其他类似帖子中可以找到的所有内容,但到目前为止没有任何效果。

控制器:

public class ClientesController implements Initializable {

    AplicacaoGPSD oApp = AplicacaoGPSD.getInstance();
    Empresa oEmpresa = oApp.getEmpresa();
    RegistoClientes oRegistoClientes = oEmpresa.getRegistoClientes();

    @FXML
    private TableView<Cliente> tableClientes;

    @FXML
    private TableColumn<Cliente, String> colNome;

    @FXML
    private TableColumn<Cliente, String> colNif;

    @FXML
    private TableColumn<Cliente, String> colEmail;

    @FXML
    private TableColumn<Cliente, String> colTelefone;

    @FXML
    private TableColumn<Cliente, EnderecoPostal> colMorada;

    @FXML
    private TableColumn<Cliente, String> colPassword;

    public ClientesController() {
    }

    public void initialize(URL location, ResourceBundle resources) {
        updateClientes();
    }

    private void updateClientes() {
        colNome.setCellValueFactory(new PropertyValueFactory<Cliente, String>("m_strNome"));
        colNif.setCellValueFactory(new PropertyValueFactory<Cliente, String>("m_strNif"));
        colEmail.setCellValueFactory(new PropertyValueFactory<Cliente, String>("m_strEmail"));
        colTelefone.setCellValueFactory(new PropertyValueFactory<Cliente, String>("m_strTelefone"));
        ObservableList<Cliente> list = FXCollections.observableArrayList(oRegistoClientes.getClientes());
        tableClientes.setItems(list);
    }
}

客户端模型类:

public class Cliente {

    private String m_strNome;
    private String m_strNIF;
    private String m_strTelefone;
    private String m_strEmail;
    private List<EnderecoPostal> m_lstMoradas = new ArrayList<EnderecoPostal>();

    public Cliente(String strNome, String strNIF, String strTelefone, String strEmail, EnderecoPostal oMorada) {
        if ((strNome == null) || (strNIF == null) || (strTelefone == null) || (strEmail == null) 
                //|| (oMorada == null)
                || (strNome.isEmpty()) || (strNIF.isEmpty()) || (strTelefone.isEmpty()) || (strEmail.isEmpty()))
            throw new IllegalArgumentException("Nenhum dos argumentos pode ser nulo ou vazio.");

        this.m_strNome = strNome;
        this.m_strEmail = strEmail;
        this.m_strNIF = strNIF;
        this.m_strTelefone = strTelefone;
        m_lstMoradas.add(oMorada);
    }

    public String getNome() {
        return this.m_strNome;
    }

    public String getEmail() {
        return this.m_strEmail;
    }

    public String getNif() {
        return this.m_strNIF;
    }

    public boolean hasEmail(String strEmail) {
        return this.m_strEmail.equalsIgnoreCase(strEmail);
    }

    public boolean hasNif(String strNIF) {
        return this.m_strNIF.equalsIgnoreCase(strNIF);
    }

    public boolean addEnderecoPostal(EnderecoPostal oEnderecoPostal) {
        if (validaEnderecoPostal(oEnderecoPostal))
            return addEPostal(oEnderecoPostal);
        return false;
    }

    private boolean validaEnderecoPostal(EnderecoPostal oEnderecoPostal) {
        boolean bRet = true;
        for (EnderecoPostal endPostal : m_lstMoradas) {
            if (oEnderecoPostal.equals(endPostal)) {
                bRet = false;
            }
        }
        return bRet;
    }

    private boolean addEPostal(EnderecoPostal oEnderecoPostal) {
        return this.m_lstMoradas.add(oEnderecoPostal);
    }

    public boolean removeEnderecoPostal(EnderecoPostal oMorada) {
        return this.m_lstMoradas.remove(oMorada);
    }
}

fxml文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="788.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.xxsd.pprog_pw03.controllers.ClientesController">
   <center>
      <VBox alignment="CENTER" prefHeight="431.0" prefWidth="770.0">
         <children>
            <TableView fx:id="tableClientes" prefHeight="542.0" prefWidth="768.0">
               <columns>
                  <TableColumn fx:id="colNome" prefWidth="118.0" text="Nome" />
                  <TableColumn fx:id="colNif" prefWidth="101.0" text="NIF" />
                  <TableColumn fx:id="colEmail" minWidth="0.0" prefWidth="123.0" text="Email" />
                  <TableColumn fx:id="colTelefone" prefWidth="104.0" text="Telefone" />
                  <TableColumn fx:id="colMorada" prefWidth="135.0" text="Endereço Postal" />
                  <TableColumn fx:id="colPassword" prefWidth="186.0" text="Password (Encriptada)" />
               </columns>
            </TableView>
         </children>
      </VBox>
   </center>
   <opaqueInsets>
      <Insets />
   </opaqueInsets>
   <padding>
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
   </padding>
   <top>
      <VBox alignment="CENTER">
         <children>
            <Label fx:id="lblTitle" styleClass="page-title" text="Clientes Registados">
               <font>
                  <Font name="System Bold" size="15.0" />
               </font></Label>
         </children>
      </VBox>
   </top>
</BorderPane>

0 个答案:

没有答案