Good day
After a lot of effort and experiments with the goal of properly implementing mvc in a swing application. I am still unable to build an Mvc swing java application.
Please Find source code below.
This is The Model
public class RentalModel implements Serializable {
private String rentalNumber;
private Date rentalDate;
private Date rentalReturnDate;
private String rentalStatus;
private EmployeeModel employeeModel;
private Integer rentalQuatity;
private VideoCopyModel copyModel;
public VideoCopyModel getCopyModel() {
return copyModel;
}
public void setCopyModel(VideoCopyModel copyModel) {
this.copyModel = copyModel;
}
public Integer getRentalQuatity() {
return rentalQuatity;
}
public void setRentalQuatity(Integer rentalQuatity) {
this.rentalQuatity = rentalQuatity;
}
public String getRentalNumber() {
return rentalNumber;
}
public void setRentalNumber(String rentalNumber) {
this.rentalNumber = rentalNumber;
}
public Date getRentalDate() {
return rentalDate;
}
public void setRentalDate(Date rentalDate) {
this.rentalDate = rentalDate;
}
public Date getRentalReturnDate() {
return rentalReturnDate;
}
public void setRentalReturnDate(Date rentalReturnDate) {
this.rentalReturnDate = rentalReturnDate;
}
public String getRentalStatus() {
return rentalStatus;
}
public void setRentalStatus(String rentalStatus) {
this.rentalStatus = rentalStatus;
}
public EmployeeModel getEmployeeModel() {
return employeeModel;
}
public void setEmployeeModel(EmployeeModel employeeModel) {
this.employeeModel = employeeModel;
}
private CustomerModel customerModel;
public CustomerModel getCustomerModel() {
return customerModel;
}
public void setCustomerModel(CustomerModel customerModel) {
this.customerModel = customerModel;
}
@Override
public String toString() {
return "RentalModel [rentalNumber=" + rentalNumber + ", rentalDate="
+ rentalDate + ", rentalReturnDate=" + rentalReturnDate
+ ", rentalStatus=" + rentalStatus + ", employeeModel="
+ employeeModel + ", rentalQuatity=" + rentalQuatity
+ ", copyModel=" + copyModel + ", customerModel="
+ customerModel + "]";
}
}
Here is my view I have left out the code that Creates the frame
package views;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import shared.ClientFacade;
public class RentMovieView extends JFrame {
private JPanel contentPane;
private JTextField txtRentalNumber;
private JTextField RentalQuantity;
private JTextField txtRentalDate;
private JTextField txtrentalStatus;
JComboBox cmbEmployeeNumber = new JComboBox();
JComboBox cmbCustomerNumber = new JComboBox();
JComboBox cmbVideoNumber = new JComboBox();
JButton btnRent = new JButton("Rent");
public JButton getBtnRent() {
return btnRent;
}
public void setBtnRent(JButton btnRent) {
this.btnRent = btnRent;
}
public JButton getBtnCancel() {
return btnCancel;
}
public void setBtnCancel(JButton btnCancel) {
this.btnCancel = btnCancel;
}
JButton btnCancel = new JButton("Cancel");
public JTextField getTxtRentalNumber() {
return txtRentalNumber;
}
public void setTxtRentalNumber(JTextField txtRentalNumber) {
this.txtRentalNumber = txtRentalNumber;
}
public JTextField getRentalQuantity() {
return RentalQuantity;
}
public void setRentalQuantity(JTextField rentalQuantity) {
RentalQuantity = rentalQuantity;
}
public JTextField getTxtRentalDate() {
return txtRentalDate;
}
public void setTxtRentalDate(JTextField txtRentalDate) {
this.txtRentalDate = txtRentalDate;
}
public JTextField getTxtrentalStatus() {
return txtrentalStatus;
}
public void setTxtrentalStatus(JTextField txtrentalStatus) {
this.txtrentalStatus = txtrentalStatus;
}
public JComboBox getCmbEmployeeNumber() {
return cmbEmployeeNumber;
}
public void setCmbEmployeeNumber(JComboBox cmbEmployeeNumber) {
this.cmbEmployeeNumber = cmbEmployeeNumber;
}
public JComboBox getCmbCustomerNumber() {
return cmbCustomerNumber;
}
public void setCmbCustomerNumber(JComboBox cmbCustomerNumber) {
this.cmbCustomerNumber = cmbCustomerNumber;
}
public JComboBox getCmbVideoNumber() {
return cmbVideoNumber;
}
public void setCmbVideoNumber(JComboBox cmbVideoNumber) {
this.cmbVideoNumber = cmbVideoNumber;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RentMovieView frame = new RentMovieView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Here is the controller class
public class RentalController {
private RentalModel model;
private RentMovieView view;
private ActionListener actionListener;
public RentalController(RentalModel model, RentMovieView view)
{
this.model = model;
this.view = view;
}
public void control()
{
actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.setRentalNumber(view.getTxtRentalNumber().getText());
model.setRentalQuatity(Integer.parseInt(view.getRentalQuantity().getText()));
model.setRentalStatus(view.getTxtrentalStatus().getText());
Date rentDate = new Date(view.getTxtRentalDate().getText().toString());
model.setRentalDate(rentDate);
rentMovie();
}
};
view.getBtnRent().addActionListener(actionListener);
}
public void rentMovie()
{
ClientManager clientManager = new ClientManager();
List<RentalModel> models = new ArrayList<>();
models.add(model);
clientManager.sendRent(models);
}
}
And finally this is the Class that puts all together
public class ClientFacade {
static RentalModel model = new RentalModel();
static RentMovieView view = new RentMovieView();
public static void main(String[] args) {
rentMovie();
}
public static void rentMovie()
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
view = new RentMovieView();
view.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
controllers.RentalController controller = new RentalController(model, view);
controller.control();
}
}
Please help me find the correct way of implementing this pattern.
Thanks In advance