【问题标题】:JavaFX tableview, my list is erased when I add a new objectJavaFX tableview,当我添加一个新对象时我的列表被删除
【发布时间】:2017-10-24 15:50:33
【问题描述】:

我在 JavaFX 中遇到了 Tableview 的问题。

每当我在 Tableview 中添加新行时,我的列表都会被删除。

我预先制作了一个包含 4 个对象的列表 当我使用我的按钮添加一行时,该行被添加。

问题是添加行的时候。如果我的一个单元格中有文本,所有内容都会被删除。

这是我的主程序:

package application;

import vue.*;
import domaine.Reponse;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group; 
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import javafx.stage.Stage;


public class Mainfx extends Application {
public static final String nomApplication = "QCM-Builder";
private TableViewReponse tableauReponse;
private Button addButton;

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

public void start(Stage stage){
    Scene scene = new Scene(new Group());
     stage.setTitle("Table View Sample");
        stage.setWidth(300);
        stage.setHeight(500);
    tableauReponse = new TableViewReponse();

    tableauReponse.setTranslateX(130);
    tableauReponse.setTranslateY(300);
    tableauReponse.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    tableauReponse.setPrefHeight(150);

    addButton = new Button("Add");
    addButton.setTranslateX(400);
    addButton.setTranslateY(400);
    ((Group) scene.getRoot()).getChildren().addAll(addButton);
    addButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override 
        public void handle(ActionEvent e) {
            ObservableList<Reponse> list = tableauReponse.getItems();
            list.add(new Reponse("",false));
            tableauReponse.setItems(list);
            }
    });

    ((Group) scene.getRoot()).getChildren().addAll(tableauReponse);
    stage.setScene(scene);
    stage.show();

}
}

这是我的自定义表格视图

package vue;

 import java.io.IOException;
 import java.sql.SQLException;
 import domaine.Reponse;
 import javafx.beans.property.SimpleBooleanProperty;
 import javafx.beans.value.ChangeListener;
 import javafx.beans.value.ObservableValue;
 import javafx.collections.FXCollections; 
 import javafx.collections.ObservableList;
 import javafx.geometry.Insets;
 import javafx.geometry.Pos;
 import javafx.scene.control.TableCell;
 import javafx.scene.control.TableColumn;
 import javafx.scene.control.TableColumn.CellDataFeatures;
 import javafx.scene.control.TableColumn.CellEditEvent;
 import javafx.scene.control.TablePosition;
 import javafx.scene.control.TableView;
 import javafx.scene.control.cell.CheckBoxTableCell;
 import javafx.scene.control.cell.PropertyValueFactory;
 import javafx.scene.control.cell.TextFieldTableCell;
 import javafx.scene.layout.StackPane;
 import javafx.util.Callback;

public class TableViewReponse extends TableView<Reponse> {

private TableColumn<Reponse, String> reponseCol;
private TableColumn<Reponse, Boolean> singleCol;
private final ObservableList<Reponse> list =
        FXCollections.observableArrayList(
        new Reponse("",false),
        new Reponse("",false),
        new Reponse("",false),
        new Reponse("",false)
        );

public TableViewReponse() {
    super();

    this.setEditable(true);
    reponseCol = new TableColumn<Reponse, String>("Réponse");

    singleCol = new TableColumn<Reponse, Boolean>("Correcte ?");

    // ==== FULL NAME (TEXT FIELD) ===

    reponseCol.setCellValueFactory(new PropertyValueFactory<Reponse, String>("reponse"));

    reponseCol.setCellFactory(TextFieldTableCell.<Reponse>forTableColumn());

    reponseCol.setMinWidth(200);

    // On Cell edit commit (for FullName column)
    reponseCol.setOnEditCommit((CellEditEvent<Reponse, String> event) -> {
        TablePosition<Reponse, String> pos = event.getTablePosition();

        int row = pos.getRow();
        Reponse reponse = event.getTableView().getItems().get(row);

        reponse.setLibelle(event.getNewValue());

        try {
            System.out.println("Bisous "+ reponse.toStringAMC());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    });

    // ==== SINGLE? (CHECK BOX) ===
    singleCol.setCellValueFactory(new Callback<CellDataFeatures<Reponse, Boolean>, ObservableValue<Boolean>>() {

        @Override
        public ObservableValue<Boolean> call(CellDataFeatures<Reponse, Boolean> param) {
            Reponse reponse = param.getValue();

            SimpleBooleanProperty booleanProp = new SimpleBooleanProperty(reponse.estJuste());

            // Note: singleCol.setOnEditCommit(): Not work for
            // CheckBoxTableCell.

            // When "Single?" column change.
            booleanProp.addListener(new ChangeListener<Boolean>() {

                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue,
                        Boolean newValue) {
                    reponse.setJuste(newValue);
                }
            });
            return booleanProp;
        }
    });

    singleCol.setCellFactory(new Callback<TableColumn<Reponse, Boolean>, TableCell<Reponse, Boolean>>() {
        @Override
        public TableCell<Reponse, Boolean> call(TableColumn<Reponse, Boolean> p) {
            CheckBoxTableCell<Reponse, Boolean> cell = new CheckBoxTableCell<Reponse, Boolean>();
            cell.setAlignment(Pos.CENTER);
            //cell.commitEdit(true);
            return cell;
        }
    });

    this.setItems(list);
    this.getColumns().addAll(reponseCol, singleCol);

    StackPane root = new StackPane();
    root.setPadding(new Insets(5));
}


public void ajouterReponse() {
    ObservableList<Reponse> list = this.getItems();
    list.add(new Reponse("", false));
    this.setItems(list);
}

public String getColReponse(int i) {
    return reponseCol.getTableView().getItems().get(i).getLibelle();
}

public void viderColReponse(int i) {
    reponseCol.getTableView().getItems().get(i).setLibelle(null);
}

public Boolean getColSingle(int i) {
    return this.singleCol.getCellData(i);
}
}

任何想法为什么? 在此先感谢

【问题讨论】:

    标签: list button javafx tableview


    【解决方案1】:

    由于您没有展示您的 Reponse 课程,我只能假设,但我认为问题可能来自

    reponseCol.setCellValueFactory(new PropertyValueFactory<Reponse, String>("reponse"));
    

    以及你正在做的事实:

    reponseCol.setOnEditCommit((CellEditEvent<Reponse, String> event) -> {
        //Some code
        reponse.setLibelle(event.getNewValue());
        //the rest
    });
    

    假设您使用的是通用命名系统作为 setter/getter 您的 Reponse 类应该有一个 libelle 字段,您在提交单元格时正在更新该字段。

    另一方面,您告诉列搜索要在名为 reponse 的属性中显示的值(可能是您类的另一个字段?)。

    您更新的值与您要求在自动刷新时显示的值不同,这就是为什么当您添加新值时该列是清晰的。

    要修复它,您可以这样做:

    reponseCol.setCellValueFactory(new PropertyValueFactory<Reponse, String>("libelle"));
    

    改为在提交时显示诽谤或更新reponse 字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 2021-02-02
      相关资源
      最近更新 更多