无法在javafx中绑定HTMLEditor和TextField

时间:2014-05-20 09:10:13

标签: java binding javafx tableview html-editor

在我的GUI中,我有一个TABLEVIEW和一个HTMLEditor。

TableView有两列。第1列没有编辑功能。第二列有编辑选项。

对于HTMLEditor我写了setOnKeyPressed属性。在该属性中,在句柄方法中,我正在尝试更新第二列数据。但是TableView Control中的数据根本没有反映出来。但是当我尝试打印第二列的数据时,它只显示HTMLEditor的数据。但是HTMLEditor的数据没有反映在TableView第二列中。

以下是我的代码

public class BinderDocumnetController implements Initializable {


    public BinderDocumnetController() {
    this.details = FXCollections.observableArrayList();
}    


@Override
public void initialize(URL url, ResourceBundle rb) {
    tblName.setCellValueFactory(new PropertyValueFactory<CData, String>("strName"));
    tblRemarks.setCellValueFactory(new PropertyValueFactory<CData, String>("strRemarks"));
    tblData.setItems(details);
    tblData.setEditable(true);

     Callback<TableColumn<CData, String>, TableCell<CData, String>> para4cellFactory
            = new Callback<TableColumn<CData, String>, TableCell<CData, String>>() {
                public TableCell call(TableColumn p) {
                    return new EditingCell();
                }
            };

    tblRemarks.setCellFactory(para4cellFactory);


        htmlEditor.setOnKeyReleased(new EventHandler<KeyEvent>()
                {

            @Override
            public void handle(KeyEvent t) {
                textField.setText(htmlEditor.getHtmlText());
                System.out.println("row no. is "+tblData.getSelectionModel().getSelectedIndex());
                tblData.getItems().get(tblData.getSelectionModel().getSelectedIndex()).setStrRemarks(htmlEditor.getHtmlText());
                System.out.println(" data at row no.  "+tblData.getSelectionModel().getSelectedIndex()+"is");
                System.out.println(" "+tblData.getItems().get(tblData.getSelectionModel().getSelectedIndex()).getStrRemarks());
            }

                }
        );

    textField.setOnKeyPressed(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent t) {
            htmlEditor.setHtmlText(textField.getText());
        }

    });

}

public void onbtnAddClicked(){
    System.out.println(" in add");
    CData cdata= new CData("XYZ","done");
    details.add(cdata);

}

@FXML
HTMLEditor htmlEditor;

@FXML
TextField textField;

ObservableList<CData> details;

@FXML
TableView<CData> tblData;

@FXML
TableColumn<CData,String> tblRemarks,tblName;

@FXML
Button btnAdd;





class EditingCell extends TableCell<Object, String> {

    private TextField textField;

    public EditingCell() {
    }

    @Override
    public void startEdit() {
        if (!isEmpty()) {
            super.startEdit();
            createTextField();
            setText(null);
            setGraphic(textField);
            textField.selectAll();
        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();

        setText((String) getItem());
        setGraphic(null);
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());
                }
                setText(null);
                setGraphic(textField);
            } else {
                setText(getString());
                setGraphic(null);
            }
        }
    }

    private void createTextField() {
        textField = new TextField(getString());
        textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
        textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> arg0,
                    Boolean arg1, Boolean arg2) {
                if (!arg2) {
                    commitEdit(textField.getText());
                }
            }

        });

        textField.setOnKeyPressed(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent t) {
            htmlEditor.setHtmlText(textField.getText());
        }

    });
    }

    private String getString() {
        return getItem() == null ? "" : getItem().toString();
    }
}

}

以下是CData.java代码

public class CData {
SimpleStringProperty strName = new SimpleStringProperty("");
SimpleStringProperty strRemarks = new SimpleStringProperty("");


public CData(String strName,String strRemarks) {
    setStrName(strName);
    setStrRemarks(strRemarks);

}

public String getStrName() {
    return strName.get();
}

public void setStrName(String strName) {
    this.strName.set(strName);
}

public String  getStrRemarks() {
    return strRemarks.get();
}

public void setStrRemarks(String strRemarks) {
    this.strRemarks.set(strRemarks);
}

public SimpleStringProperty strName() {
    return strName;
}

public SimpleStringProperty strRemarks() {
    System.out.println("\nRemarks: "+strRemarks.get()+"\n\n");
    return strRemarks;
}

}

1 个答案:

答案 0 :(得分:0)

public SimpleStringProperty strNameProperty() {
return strName;
}

 public SimpleStringProperty strRemarksProperty() {
System.out.println("\nRemarks: "+strRemarks.get()+"\n\n");
return strRemarks;
}

现在对上面的代码进行了更改