【问题标题】:tableview is empty when i add an item添加项目时tableview为空
【发布时间】:2016-12-23 16:07:31
【问题描述】:

我需要空表视图方面的帮助。 当我将项目放入 ObservableList 并使用此列表执行 setItems 时,tableview 不会显示列表中的项目。

我已经包含了最小、完整和可验证的示例(我认为)。

主类。

 public class GeneralController implements Initializable {

    @FXML
    private TableView<Subvencio> tabla;
    @FXML
    private TableColumn<Subvencio, Double> ctotal;
     @FXML
    private TableColumn<Subvencio, String> cesport;
    ObservableList<Subvencio> subs = FXCollections.observableArrayList();

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        cesport.setCellValueFactory(new PropertyValueFactory<Subvencio, String>("Esport"));
        ctotal.setCellValueFactory(new PropertyValueFactory<Subvencio, Double>("Total"));
        tabla.setItems(subs);
    }

    //Mètode per a afegir una subvenció a la llista(Mitjansant un scene diferent i un showandWait).
    @FXML
    private void afegir(ActionEvent event) throws IOException {
        //Carregem el controlador i inicialitzem la finestra a obrir.
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/model/Afegir.fxml"));
        Parent root = loader.load();
        Scene scene = new Scene(root);
        Stage ventana2= new Stage();
        ventana2.setMinHeight(380);
        ventana2.getIcons().add(new Image("/estil/Boo.png"));
        ventana2.setMinWidth(770);
        ventana2.setTitle("Afegir dades");
        ventana2.initModality(Modality.APPLICATION_MODAL);
        ventana2.setScene(scene);
        //Fem que espere fins a ser tancada i coloquem els elements a la taula.subs = new ObservableList<Subvencio>();
        ventana2.showAndWait();
        tabla.setItems(subs);
}

Afegir 类。

   public class AfegirController implements Initializable {

    @FXML
    private TextField esport;
    @FXML
    private TextField equips;
    @FXML
    private TextField despeses;
    @FXML
    private TextField esportistes;
    @FXML
    private TextField puntuacio;
    @FXML
    private TextField socis;
    @FXML
    private CheckBox festes;
    @FXML
    private CheckBox disiplina;
    @FXML
    private CheckBox voluntari;
    @FXML
    private CheckBox instalacions;
    @FXML
    private Button bafegir;
    Subvencio sub = new Subvencio();
    
    //Metode per a afegir una subvencio a la llista de elements.
    @FXML
    private void afegir(ActionEvent event) {
        //Comprobem si tots els textfielsd estan omplits, sino es així, mostrem un error.
         if ((!equips.getText().isEmpty())
            && (equips.getText().trim().length()!=0)
            && (!despeses.getText().isEmpty())
            && (despeses.getText().trim().length()!=0)
                && (!esportistes.getText().isEmpty())
            && (esportistes.getText().trim().length()!=0)
                && (!puntuacio.getText().isEmpty())
            && (puntuacio.getText().trim().length()!=0)
                && (!socis.getText().isEmpty())
            && (socis.getText().trim().length()!=0)
                && (!esport.getText().isEmpty())
            && (esport.getText().trim().length()!=0))
         { 
             //Passem els valors de el textfield als seus corresponents formats.
            int aux1 = Integer.parseInt(equips.getText());
            double aux2 = Double.parseDouble(despeses.getText());
            int aux3 = Integer.parseInt(esportistes.getText());
            int aux4 = Integer.parseInt(puntuacio.getText());
            int aux5 = Integer.parseInt(socis.getText());
            boolean aux6 = festes.isSelected();
            boolean aux7 = disiplina.isSelected();
            boolean aux8 = voluntari.isSelected();
            boolean aux9 = instalacions.isSelected();
            String aux10 = esport.getText();
           //Carregem un controllardor per poder crear subvencions i la creeem. en cabar aques proces la finestra es tanca.
           GeneralController aux = new GeneralController();
           sub.setEsport(aux10);
           sub.setEquips(aux1);
           sub.setEsportistes(aux3);
           sub.setFestes(aux6);
           sub.setFormacio(aux7);
           sub.setInstalacions(aux9);
           sub.setPuntuacio(aux4);
           sub.setSocis(aux5);
           sub.setVoluntari(aux8);
           sub.setdespeses(aux2);
           aux.subs.add(sub);
           Node minodo = (Node) event.getSource(); 
           minodo.getScene().getWindow().hide();
         }
         else {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            //Apliquem el css cridant al metode dialogpane
            DialogPane dialogPane = alert.getDialogPane();
            dialogPane.getStylesheets().add(getClass().getResource("/estil/flatterfx.css").toExternalForm());
            dialogPane.getStyleClass().add("button");
            Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
            stage.getIcons().add(
                new Image(this.getClass().getResource("/estil/Boo.png").toString()));
            alert.setTitle("Error");
            alert.setHeaderText("Error al afegir una subvenció.");
            alert.setContentText("Deus emplenar tots els cuadres de text per poder afegir una subvenció.");
            Optional<ButtonType> result = alert.showAndWait();
         
         }
    }
    public void inicializaSubvencio (Subvencio aux){
        sub = aux;
        String aux1 = String.valueOf(aux.getEsport());
        String aux2 = String.valueOf(aux.getEquips());
        String aux3 = String.valueOf(aux.getDespeses());
        String aux4 = String.valueOf(aux.getEsportistes());
        String aux5 = String.valueOf(aux.getEsportistes());
        String aux6 = String.valueOf(aux.getPuntuacio());
        String aux7 = String.valueOf(aux.getSocis());
        String aux8 = String.valueOf(aux.getTotal());
        double total = aux.getTotal();
        esport.setText(aux1);
        equips.setText(aux2);
        despeses.setText(aux3);
        esportistes.setText(aux5);
        puntuacio.setText(aux6);
        socis.setText(aux7);
        festes.setSelected(aux.getFestes());
        disiplina.setSelected(aux.getFormacio());
        voluntari.setSelected(aux.getVoluntari());
        instalacions.setSelected(aux.getInstalacions());
    }
}

还有 subvencio 类。

public class Subvencio {
    String esport;
    int equips;
    double despeses;
    int esportistes;
    int puntuacio;
    int socis;
    boolean festes;
    boolean formacio;
    boolean voluntari;
    boolean instalacions;
    double total;
    
    //Mètodes generals de creacio d'una subvenció.
    public Subvencio() {
        this(null, 0, 0, 0, 0, 0, 0, false, false, false, false);
    }
    
    public Subvencio(String esport, double total, double despeses, int equips, int esportistes, int puntuacio, 
            int socis, boolean festes, boolean formacio, 
            boolean voluntari, boolean instalacions) {
        this.esport = esport;
        this.total = 0;
        this.despeses = despeses;
        this.equips = equips;
        this.esportistes = esportistes;
        this.puntuacio = puntuacio;
        this.socis = socis;
        this.festes = festes;
        this.formacio = formacio;
        this.instalacions = instalacions;
        this.voluntari = voluntari;
    }
    
    //Mètodes getters i setters per poder treballar amb els valors que contè una subvenció.
    public String getEsport() {
    return esport;
    }

    public void setEsport(String esport) {
    this.esport = esport;
    }
}

编辑:@James_D:我使用您的控制器之一的实例,因为如果我不这样做,netbeans 会说我“无法从静态上下文引用非静态变量 subs”,而我的大学教授说我认为这个 rapair 那个错误。我有其他示例来自 pc 商店,我使用此修复程序,此错误未出现在示例中,但在此应用中出现。

这个应用程序的 Mi 想法是这样的: 当按下按钮并填充文本字段时,程序关闭该窗口并将 subvencio 放在第二个窗口的 tableview 中。我的问题是当我这样做时,tableview 不会在 tableview 上显示任何项目。

在我的应用中添加了一个巨型链接,用于检查所有代码并测试错误。 Subvencio App

我添加了一个链接,因为我不知道在我的最小代码中是否可以测试错误。 要重现错误,您可以打开应用程序 push afegir 按钮并填写文本字段。之后,再次按下 afegir。 谢谢你帮助我。

【问题讨论】:

  • 您能否展示inicializaSubvencio 方法和足够的Subvencio 以了解您如何实现这些属性?
  • 我没有看到 TableView 的定义以及您创建它的模板类(如果有的话)。我认为您可以显示更多代码,首先是您在这些 sn-ps 中引用的方法。
  • 关于在 TableView 中看不到数据。假设,data 是 ObservableList,tabV 是 TableView。试试下面的,tabV.setItems(data); data.add(Object of your TableView type);
  • @fabian 向我们展示我所有的程序。感谢您帮助我解决我的问题。更新tableview的问题解决了^^
  • @joseclimentpenades 这个问题是完全回答(自我回答)还是剩下的?我不完全确定。

标签: java javafx


【解决方案1】:

要解决这个问题,您可以这样做: 当您打开 2 窗口时,在 Main 类中加载正确的控制器:

 private void afegir(ActionEvent event) throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/model/Afegir.fxml"));
    Parent root = loader.load();
    //load the correct controller
    AfegirController controller = loader.getController();
    Scene scene = new Scene(root);
    Stage ventana2= new Stage();
    ventana2.setMinHeight(380);
    ventana2.getIcons().add(new Image("/estil/Boo.png"));
    ventana2.setMinWidth(770);
    ventana2.setTitle("Afegir dades");
    ventana2.initModality(Modality.APPLICATION_MODAL);
    ventana2.setScene(scene);
    taula.subs = new ObservableList<Subvencio>();
    ventana2.showAndWait();
    //Add the sub to Observable list here and add the observableList to tableview.
    subs.add(controller.getSub());
    tabla.setItems(subs);
}

接下来在 Afegir 类中执行以下操作:

 public Subvencio getSub() {
    return sub;
}

这是一种公共方法,可以访问我喜欢添加到列表中的 sub。 最后在 afegir Class 删除这一行

GeneralController aux = new GeneralController();
aux.subs.add(sub);

我这样做了,我的 tableview 正确地显示了这些项目。 谢谢你的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 2017-12-20
    • 2021-09-11
    相关资源
    最近更新 更多