JavaFX Tableview没有绑定/还原

时间:2015-07-07 13:28:44

标签: javafx tableview

寻找帮助诊断行为的方法,因为我对它可能发生的地方感到茫然。

在表格编辑上,标签会更新,但似乎看不到值,因为每当表格更新时,值都会恢复。这种情况发生在排序中,并且可以通过以下方式显示为:

for(Row r : elementsTable.getItems().get(0))
   System.out.println(r.getSymbol() + r.getWeight() + r.getAtom());

GUI:

@FXML
private TextField row1;
@FXML
private TextField row2;
@FXML
private TextField row3;
@FXML
private TableView<Row> elementsTable;
private List<Row> row;
private ObservableList<Row> oRow;

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    row = new ArrayList();
}  

private void addRow(ActionEvent event) {    
 elementsTable.getItems().add(new Row(
         row1.getText(),
         row2.getText(),
         row3.getText()));
}

public void
construct(List<String> z, List<String> w, List<String> z)
throws Exception{
    //populate table rows
    for(int i = 0; i < z.size(); i++){
        row.add(new Row(z.get(i), w.get(i), a.get(i)));
    }
    oRow = FXCollections.observableArrayList(row);
    elementsTable.setItems(oRow);

}

FXML

  <TableView fx:id="elementsTable" editable="true" layoutX="67.0" layoutY="124.0" prefHeight="237.0" prefWidth="523.0">
     <columns>
        <TableColumn fx:id="elementCol" editable="true" prefWidth="138.0" text="Element">
           <cellFactory>
              <TextFieldTableCell fx:factory="forTableColumn" />
           </cellFactory>
           <cellValueFactory>
              <PropertyValueFactory property="symbol" />
           </cellValueFactory>
        </TableColumn>
        <TableColumn fx:id="weightCol" editable="true" prefWidth="191.0" text="Weight Fraction">
           <cellFactory>
              <TextFieldTableCell fx:factory="forTableColumn" />
           </cellFactory>
           <cellValueFactory>
              <PropertyValueFactory property="weight" />
           </cellValueFactory>
        </TableColumn>
        <TableColumn fx:id="atomCol" editable="true" prefWidth="189.0" text="Atom Fraction">
           <cellFactory>
              <TextFieldTableCell fx:factory="forTableColumn" />
           </cellFactory>
           <cellValueFactory>
              <PropertyValueFactory property="atom" />
           </cellValueFactory>
        </TableColumn>
     </columns>
     <items>
        <FXCollections fx:factory="observableArrayList">
           <Row atom="" symbol="" weight="" />
        </FXCollections>
     </items>
  </TableView>

表格型号

public class Row{
    private final SimpleStringProperty symbol = new SimpleStringProperty("");
    private final SimpleStringProperty weight = new SimpleStringProperty("");
    private final SimpleStringProperty atom = new SimpleStringProperty("");

    /**
     * Default constructor, defaults to empty strings
     */
    public Row(){
        this("","","");
    }
    /**
     * 
     * @param s symbol(or ZA)
     * @param w weight fraction
     * @param a atom fraction (or atom count ending with "#")
     */
    public Row(String s, String w, String a){
        setSymbol(s);
        setWeight(w);
        setAtom(a);
    }

    public String getSymbol(){return symbol.get();}
    public String getWeight(){return weight.get();}
    public String getAtom(){return atom.get();}

    public void setSymbol(String s){symbol.set(s);}
    public void setWeight(String w){weight.set(w);}
    public void setAtom(String a){atom.set(a);}
}

状态1:在做任何事之前 enter image description here

状态2:已编辑的单元格 enter image description here

状态3:向表中添加行,数据将恢复编辑 enter image description here

1 个答案:

答案 0 :(得分:1)

问题是你没有提供&#34;属性访问者&#34; Row课程中的方法。如果没有这些,PropertyValueFactoryTextFieldTableCell将无法访问实际属性并绑定/收听它们以进行更改。因此,Row类应该如下所示

public class Row{
    private final SimpleStringProperty symbol = new SimpleStringProperty("");
    private final SimpleStringProperty weight = new SimpleStringProperty("");
    private final SimpleStringProperty atom = new SimpleStringProperty("");

    /**
     * Default constructor, defaults to empty strings
     */
    public Row(){
        this("","","");
    }
    /**
     * 
     * @param s symbol(or ZA)
     * @param w weight fraction
     * @param a atom fraction (or atom count ending with "#")
     */
    public Row(String s, String w, String a){
        setSymbol(s);
        setWeight(w);
        setAtom(a);
    }

    public String getSymbol(){return symbol.get();}
    public String getWeight(){return weight.get();}
    public String getAtom(){return atom.get();}

    public void setSymbol(String s){symbol.set(s);}
    public void setWeight(String w){weight.set(w);}
    public void setAtom(String a){atom.set(a);}

    public StringProperty symbolProperty() {
        return symbol ;
    }

    public StringProperty weightProperty() {
        return weight ;
    }

    public StringProperty atomProperty() {
        return atom ;
    } 
}

有关详细信息,请查看API documentation for PropertyValueFactorytutorial section on properties(这只是JavaFX属性模式的一般背景)。

如果出于某种原因,您无法提供这些方法(例如,您使用的是现有的课程,则无法更改为您的模型),您可以提供&#34;接线&#34;使用控制器类中的TableColumn.setOnEditCommit(...)手动编辑表列编辑和模型之间。以下内容应该可以使您的现有Row类工作,但我建议使用&#34;属性访问器&#34;只要有可能。

public void initialize(URL url, ResourceBundle rb) {
    // ...
    elementCol.setOnEditCommit(e -> {
        int rowIndex = e.getTablePosition().getRow();
        Row row = elementsTable.getItems().get(rowIndex);
        row.setElement(e.getNewValue());
    });

    // similarly for other columns...
}