2017-05-04 23 views
0

我是JavaFX的新手,如果聽起來很愚蠢,我很抱歉。我正在開發桌面應用程序以顯示數據庫中的數據。我正在使用JPA Hibernate。如何操作我從文本字段獲取的數據並將其填充到JavaFX的表格列中

這是一個承包商和合同的數據庫,通過從輸入獲取合同的起始日期和合同期限,我希望能夠計算結束日期並將其顯示在表格中。

我創建了計算endDate的方法,將月數加到開始日期,但問題是如何將它顯示在表上。

看控制器類:

package energiadata.controller; 



public class ActiveContractTabController { 

private ObservableList<ActiveContractor> dataActive = FXCollections.observableArrayList(); 

@FXML 
private VBox vBox; 
@FXML 
private TextField tfContractor; 
@FXML 
private TextField tfNatureOfContract; 
@FXML 
private TextField tfUser_dept; 
@FXML 
private TextField tfValue; 
@FXML 
private TextField tfValueRate; 
@FXML 
public DatePicker startDatePicker; 
@FXML 
public TextField tfContractDuration; 
@FXML 
private TextField tfremarks; 
@FXML 
private ChoiceBox<String> choiceBox; 
@FXML 
private Button addContractButton; 
@FXML 
private TextArea resultAreaActive; 
@FXML 
private TableView<ActiveContractor> activeContractTable; 
@FXML 
private TableColumn<ActiveContractor, Integer> idColumn; 
@FXML 
private TableColumn<ActiveContractor, String> contractorColumn; 
@FXML 
private TableColumn<ActiveContractor, String> natureOfContractColumn; 
@FXML 
private TableColumn<ActiveContractor, String> user_deptColumn; 
@FXML 
private TableColumn<ActiveContractor, Integer> valueNairaColumn; 
@FXML 
private TableColumn<ActiveContractor, Integer> valueDollarColumn; 
@FXML 
private TableColumn<ActiveContractor, LocalDate> start_dateColumn; 
@FXML 
private TableColumn<ActiveContractor, LocalDate> end_dateColumn; 
@FXML 
private TableColumn<ActiveContractor, String> contractTypeColumn; 
@FXML 
private TableColumn<ActiveContractor, String> remarksColumn; 
@FXML 
private Button allContractsButton; 
@FXML 
private Button updateButton; 
@FXML 
private Button deleteButton; 

public void setupTableActive() { 
    idColumn.setCellValueFactory(cellData -> cellData.getValue().idAProperty().asObject()); 
    contractorColumn.setCellValueFactory(cellData -> cellData.getValue().contractor()); 
    natureOfContractColumn.setCellValueFactory(cellData -> cellData.getValue().natureOfContract()); 
    user_deptColumn.setCellValueFactory(cellData -> cellData.getValue().user_dept()); 
    valueNairaColumn.setCellValueFactory(cellData -> cellData.getValue().value().asObject()); 
    valueDollarColumn.setCellValueFactory(cellData -> cellData.getValue().valueMain().asObject()); 
    start_dateColumn.setCellValueFactory(cellData -> cellData.getValue().start_date()); 
    end_dateColumn.setCellValueFactory(cellData -> cellData.getValue().end_date()); 
    contractTypeColumn.setCellValueFactory(cellData -> cellData.getValue().contractType()); 
    remarksColumn.setCellValueFactory(cellData -> cellData.getValue().remarks()); 
} 

@FXML 
void addButton(ActionEvent actionEvent) { 

    String contractor = tfContractor.getText(); 
    String natureOfContract = tfNatureOfContract.getText(); 
    String user_dept = tfUser_dept.getText(); 
    Integer value = Integer.parseInt(tfValue.getText()); 
    Integer valueRate = Integer.parseInt(tfValueRate.getText()); 
    LocalDate start_date = startDatePicker.getValue(); 
    Integer contractDuration = Integer.parseInt(tfContractDuration.getText()); 
    String contractType = choiceBox.getValue(); 
    String remarks = tfremarks.getText(); 

    ActiveContractor activeContractor = new ActiveContractor(contractor, natureOfContract, user_dept, value, valueRate, start_date, contractDuration, remarks, contractType); 

    try { 
     activeContractTable.getItems().add(activeContractor); 

     DBUtil.saveActiveContractor(activeContractor); 

     resultAreaActive.setText("Active Contract Added"); 
    } catch (Exception e) { 
     resultAreaActive.setText("Error adding active contract" + e); 
    } 

} 

@FXML 
void deleteActiveContractor(ActionEvent actionEvent) { 
    activeContractTable.getItems().remove(activeContractTable.getSelectionModel().getSelectedItem()); 

    DBUtil.removeActiveContractor(activeContractTable.getSelectionModel().getSelectedItem()); 
    resultAreaActive.setText(" Contractor Deleted! "); 

} 

@FXML 
void updateActiveContractorDetails(ActionEvent actionEvent) { 
    ActiveContractor activeContractor = activeContractTable.getSelectionModel().getSelectedItem(); 

    String _contractor = tfContractor.getText(); 
    String _natureOfContract = tfNatureOfContract.getText(); 
    String _user_dept = tfUser_dept.getText(); 
    Integer _value = Integer.parseInt(tfValue.getText()); 
    Integer _valueRate = Integer.parseInt(tfValueRate.getText()); 
    LocalDate _start_date = startDatePicker.getValue(); 
    Integer _contractDuration = Integer.parseInt(tfContractDuration.getText()); 
    String _remarks = tfremarks.getText(); 

    try { 
     DBUtil.updateActiveContractor(activeContractor); 

     activeContractTable.getItems().add(activeContractor); 
     activeContractTable.toFront(); 

     resultAreaActive.setText(_contractor + "updated Successfully!"); 
    } catch (Exception e) { 
     resultAreaActive.setText("Could not update the Database" + e); 
    } 
} 

@FXML 
void getActiveContractorList(ActionEvent actionEvent) { 
    EntityManager em = DBUtil.getEntityManager(); 
    List<ActiveContractor> list = em.createQuery("SELECT a FROM ActiveContractor a", ActiveContractor.class).getResultList(); 
    if (dataActive == null) { 
     dataActive = FXCollections.observableArrayList(list); 
    } else { 
     dataActive.clear(); 
     dataActive.addAll(list); 
    } 

    activeContractTable.setItems(dataActive); 
    // contractorTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 

    setupTableActive(); 

    resultAreaActive.setText(" List Display! "); 

} 

ActiveContractor activeContract; 

public LocalDate changeEndDateProperty() { 
     LocalDate localdate = startDatePicker.getValue(); 
    LocalDate end_localDate = localdate.plusMonths(Long.valueOf(tfContractDuration.getText())); 

    return end_localDate; 
} 



public Integer valueRateCal() { 
    Integer valueMainDollar = Integer.parseInt(tfValue.getText()) 
      * Integer.parseInt(tfValueRate.getText()); 
    return valueMainDollar; 
} 

@FXML 
public void initialize() { 
    setupTableActive(); 
    choiceBox.setItems(FXCollections.observableArrayList("One-Off", "Running")); 
} 

}

而實體類: 包energiadata.model;

public class ActiveContractor implements Externalizable { 

private static final long serialVersionUID = 1L; 

private IntegerProperty idA; 
private int _idA; 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Basic(optional = false) 
@Column(name = "id") 
public final int getId() { 
    if (idA == null) { 
     return _idA; 
    } else { 
     return idA.get(); 
    } 
} 

public final void setId(int id) { 
    if (this.idA == null) { 
     _idA = id; 
    } else { 
     this.idA.set(id); 
    } 
} 

public IntegerProperty idAProperty() { 
    if (idA == null) { 
     idA = new SimpleIntegerProperty(this, "id", _idA); 
    } 
    return idA; 
} 

private StringProperty contractor; 
private String _contractor; 

@Basic(optional = false) 
@Column(name = "contractor") 
public final String getContractor() { 
    if (this.contractor == null) { 

     return _contractor; 
    } else { 
     return contractor.get(); 
    } 
} 

public final void setContractor(String contractor) { 
    if (this.contractor == null) { 
     _contractor = contractor; 
    } else { 
     this.contractor.set(contractor); 
    } 

} 

public StringProperty contractor() { 
    if (contractor == null) { 
     contractor = new SimpleStringProperty(this, "contractor", _contractor); 
    } 
    return contractor; 
} 

private StringProperty natureOfContract; 
private String _natureOfContract; 

@Basic(optional = false) 
@Column(name = "natureOfContract") 
public final String getNatureOfContract() { 
    if (this.natureOfContract == null) { 
     return _natureOfContract; 
    } else { 
     return natureOfContract.get(); 
    } 
} 

public final void setNatureOfContract(String natureOfContract) { 
    if (this.natureOfContract == null) { 
     _natureOfContract = natureOfContract; 
    } else { 
     this.natureOfContract.set(natureOfContract); 
    } 

} 

public StringProperty natureOfContract() { 
    if (natureOfContract == null) { 
     natureOfContract = new SimpleStringProperty(this, "natureOfContract", _natureOfContract); 
    } 
    return natureOfContract; 
} 

private StringProperty user_dept; 
private String _user_dept; 

@Basic(optional = false) 
@Column(name = "user_dept") 
public final String getUser_dept() { 
    if (this.user_dept == null) { 
     return _user_dept; 
    } else { 
     return user_dept.get(); 
    } 
} 

public final void setUser_dept(String user_dept) { 
    if (this.user_dept == null) { 
     _user_dept = user_dept; 
    } else { 
     this.user_dept.set(user_dept); 
    } 

} 

public StringProperty user_dept() { 
    if (user_dept == null) { 
     user_dept = new SimpleStringProperty(this, "user_dept", _user_dept); 
    } 
    return user_dept; 
} 

private IntegerProperty value; 
private int _value; 

@Basic(optional = false) 
@Column(name = "value") 
public final Integer getValue() { 
    if (this.value == null) { 
     return _value; 
    } else { 
     return value.get(); 
    } 
} 

public final void setValue(Integer value) { 
    if (this.value == null) { 
     _value = value; 
    } else { 
     this.value.set(value); 
    } 

} 

public IntegerProperty value() { 
    if (value == null) { 
     value = new SimpleIntegerProperty(this, "value", _value); 
    } 
    return value; 
} 

private IntegerProperty valueMain; 
private Integer _valueMain; 

@Basic(optional = false) 
@Column(name = "valueMain") 
public final Integer getValueMain() { 
    if (this.valueMain == null) { 
     return _valueMain; 
    } else { 
     return valueMain.get(); 
    } 
} 

public final void setValueMain(Integer valueMain) { 
    if (this.valueMain == null) { 
     _valueMain = valueMain; 
    } else { 
     this.value.set(valueMain); 
    } 

} 

public IntegerProperty valueMain() { 
    if (valueMain == null) { 
     valueMain = new SimpleIntegerProperty(this, "valueMain", _valueMain); 
    } 
    return valueMain; 
} 

private IntegerProperty valueRate; 
private int _valueRate; 

@Basic(optional = false) 
@Column(name = "valueRate") 
public final Integer getValueRate() { 
    if (this.valueRate == null) { 
     return _valueRate; 
    } else { 
     return valueRate.get(); 
    } 
} 

public final void setValueRate(Integer valueRate) { 
    if (this.valueRate == null) { 
     _valueMain = valueRate; 
    } else { 
     this.value.set(valueRate); 
    } 

} 

public IntegerProperty valueRate() { 
    if (valueMain == null) { 
     valueMain = new SimpleIntegerProperty(this, "valueMain", _valueRate); 
    } 
    return valueRate; 
} 

private SimpleObjectProperty<LocalDate> start_date; 
private LocalDate _start_date; 

@Basic(optional = false) 
@Column(name = "start_date") 
public final LocalDate getStart_date() { 
    if (this.start_date == null) { 
     return _start_date; 
    } else { 
     return start_date.get(); 
    } 
} 

public final void setStart_date(LocalDate start_date) { 
    if (this.start_date == null) { 
     _start_date = start_date; 
    } else { 
     this.start_date.set(start_date); 
    } 

} 

public SimpleObjectProperty start_date() { 
    if (start_date == null) { 
     start_date = new SimpleObjectProperty<>(this, "start_date", _start_date); 
    } 
    return start_date; 
} 

private ObjectProperty<LocalDate> end_date; 
private LocalDate _end_date; 

@Basic(optional = false) 
@Column(name = "end_date") 
public final LocalDate getEnd_date() { 
    if (this.end_date == null) { 
     return _end_date; 
    } else { 
     return end_date.get(); 
    } 
} 

public final void setEnd_date(LocalDate end_date) { 
    if (this.end_date == null) { 
     _end_date = end_date; 
    } else { 
     this.end_date.set(end_date); 
    } 

} 

public ObjectProperty end_date() { 
    if (end_date == null) { 
     end_date = new SimpleObjectProperty<>(this, "end_date", _end_date); 

    } 

    return end_date; 
} 

private IntegerProperty contractDuration; 
private Integer _contractDuration; 

@Basic(optional = false) 
@Column(name = "contractDuration") 
public final Integer getContractDuration() { 
    if (this.contractDuration == null) { 
     return _contractDuration; 
    } else { 
     return contractDuration.get(); 
    } 
} 

public final void setContractDuration(Integer contractDuration) { 
    if (this.contractDuration == null) { 
     _contractDuration = contractDuration; 
    } else { 
     this.value.set(contractDuration); 
    } 

} 

public IntegerProperty contractDuration() { 
    if (contractDuration == null) { 
     contractDuration = new SimpleIntegerProperty(this, "contractDuration", _contractDuration); 

    } 
    return contractDuration; 
} 

private StringProperty remarks; 
private String _remarks; 

@Basic(optional = false) 
@Column(name = "remarks") 
public final String getRemarks() { 
    if (this.remarks == null) { 
     return _remarks; 
    } else { 
     return remarks.get(); 
    } 
} 

public final void setRemarks(String remarks) { 
    if (this.remarks == null) { 
     _remarks = remarks; 
    } else { 
     this.remarks.set(remarks); 
    } 

} 

public StringProperty remarks() { 
    if (remarks == null) { 
     remarks = new SimpleStringProperty(this, "remarks", _remarks); 
    } 
    return remarks; 
} 

private StringProperty contractType; 
private String _contractType; 

@Basic(optional = false) 
@Column(name = "contractType") 
public final String getContractType() { 
    if (this.contractType == null) { 
     return _contractType; 
    } else { 
     return contractType.get(); 
    } 
} 

public final void setContractType(String contractType) { 
    if (this.contractType == null) { 
     _contractType = contractType; 
    } else { 
     this.contractType.set(contractType); 
    } 

} 

public StringProperty contractType() { 
    if (contractType == null) { 
     contractType = new SimpleStringProperty(this, "contractType", _contractType); 
    } 
    return contractType; 
} 

public ActiveContractor() { 
} 

public ActiveContractor(String _contractor, String _natureOfContract, String _user_dept, int _value, int _valueRate, LocalDate _start_date, Integer _contractDuration, String _remarks, String _contractType) { 
    this._contractor = _contractor; 
    this._natureOfContract = _natureOfContract; 
    this._user_dept = _user_dept; 
    this._value = _value; 
    this._valueRate = _valueRate; 
    this._start_date = _start_date; 
    this._contractDuration = _contractDuration; 
    this._remarks = _remarks; 
    this._contractType = _contractType; 
} 

public ActiveContractor(StringProperty contractor, StringProperty natureOfContract, StringProperty user_dept, IntegerProperty value, IntegerProperty valueRate, SimpleObjectProperty<LocalDate> start_date, IntegerProperty contractDuration, StringProperty remarks, StringProperty contractType) { 
    this.contractor = contractor; 
    this.natureOfContract = natureOfContract; 
    this.user_dept = user_dept; 
    this.value = value; 
    this.valueRate = valueRate; 
    this.start_date = start_date; 
    this.contractDuration = contractDuration; 
    this.remarks = remarks; 
    this.contractType = contractType; 
} 

@Override 
public void writeExternal(ObjectOutput out) throws IOException { 
    out.writeInt(getId()); 
    out.writeObject(getContractor()); 
    out.writeObject(getNatureOfContract()); 
    out.writeObject(getUser_dept()); 
    out.writeObject(getValue()); 
    out.writeObject(getValueMain()); 
    out.writeObject(getValueRate()); 
    out.writeObject(getStart_date()); 
    out.writeObject(getEnd_date()); 
    out.writeObject(getContractDuration()); 
    out.writeObject(getRemarks()); 
    out.writeObject(contractType()); 

} 

@Override 
public void readExternal(ObjectInput in) throws IOException, 
     ClassNotFoundException { 
    setId(in.readInt()); 
    setContractor((String) in.readObject()); 
    setNatureOfContract((String) in.readObject()); 
    setUser_dept((String) in.readObject()); 
    setValue((Integer) in.readObject()); 
    setValueMain((Integer) in.readObject()); 
    setValueRate((Integer) in.readObject()); 
    setStart_date((LocalDate) in.readObject()); 
    setEnd_date((LocalDate) in.readObject()); 
    setContractDuration((Integer) in.readObject()); 
    setRemarks((String) in.readObject()); 
    setContractType((String) in.readObject()); 

} 

} 由於沒有實體的方法,我不能把它添加到列......我一直在這幾天。

我的問題不同於How can I add rows and columns to a JavaFX 8 TableView

我已經添加列到我的tableview,檢查我上面的setupTableActive()方法。我想要做的是,例如,end_dateColumn,我希望能夠獲得價值的DatePicker,並添加一些月份,然後將其設置爲end_dateColumn。但是默認情況下,它只允許我將Property方法放在實體類中。希望這已經足夠清晰了......

+0

可能的重複[如何添加行和列到JavaFX 8 TableView](http://stackoverflow.com/questions/25395016/how-can-i-add-rows-and-columns-to-a -javafx-8-tableview) – sorifiend

+0

這不是我要求的。再次閱讀我的文章。 – Tunde

+0

我仍然沒有正確理解你的問題,然後,爲了更好的幫助你需要向我們展示更多的代碼。什麼方法不是實體?你有錯誤可以告訴我們嗎? – sorifiend

回答

0

好的,我看到很多事情在這裏需要一些工作,但讓我們看看你的問題,你可以自己分類。從我可以告訴它看起來你永遠不會將你的end_date傳遞給activeContractTable TableView,因此無論你嘗試多麼努力,end_date都不會被添加到表中,但是你可以通過添加和調整幾行

注意我的意見開始//======

在ActiveContractTabController類Add按鈕方法:

@FXML 
void addButton(ActionEvent actionEvent) { 
    String contractor = tfContractor.getText(); 
    String natureOfContract = tfNatureOfContract.getText(); 
    String user_dept = tfUser_dept.getText(); 
    Integer value = Integer.parseInt(tfValue.getText()); 
    Integer valueRate = Integer.parseInt(tfValueRate.getText()); 
    LocalDate start_date = startDatePicker.getValue(); 
    //====== You need to add the end date here =====// 
    LocalDate end_date = changeEndDateProperty(); 
    Integer contractDuration = Integer.parseInt(tfContractDuration.getText()); 
    String contractType = choiceBox.getValue(); 
    String remarks = tfremarks.getText(); 

    //====== You need to add the end date to the line below, and modify ActiveContractor to receive the extra end_date variable =====// 
    ActiveContractor activeContractor = new ActiveContractor(contractor, natureOfContract, user_dept, value, valueRate, start_date, end_date, contractDuration, remarks, contractType); 

在ActiveContractor類:

,如下圖所示代碼

我還沒有測試或檢查過這個代碼,因此您可能需要對其進行其他調整才能按預期工作,但它應該給您一個想法。

+0

它的工作原理...你拯救我的一天。 – Tunde

相關問題