i have a combobox.it has category name.so i need to pass string to category. Category category = (Category)cmbCategory.getSelectedItem(); this line get error saying cant cast string to category type..
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
double amount = Double.parseDouble(txtAmount.getText());
Expense expense = new Expense();
expense.setReason(txtReason.getText());
expense.setAmount(amount);
DateFormat format = new SimpleDateFormat("MMMM d, yyyy");
Category category = (Category)cmbCategory.getSelectedItem();
String startDate = ((JTextField)txtDate.getDateEditor().getUiComponent()).getText();
try {
Date date = format.parse(startDate);
expense.setDate(date);
} catch (ParseException ex) {
Logger.getLogger(ExpenseView.class.getName()).log(Level.SEVERE, null, ex);
}
expense.setCategory(category);
ExpenseController.saveExpense(expense);
}
cmbCategory.getSelectedItem();String?getSelectedItem()is going to give you aCategoryor subclass ofCategory, whichStringis not.Category.forName(String)method, which returns aCategorybased on the givenString.