Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
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

Java
/**
 * @author soffo
 * 
 */
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

Java
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");
	
	/**
	 * @return the btnRent
	 */
	public JButton getBtnRent() {
		return btnRent;
	}

	/**
	 * @param btnRent the btnRent to set
	 */
	public void setBtnRent(JButton btnRent) {
		this.btnRent = btnRent;
	}

	/**
	 * @return the btnCancel
	 */
	public JButton getBtnCancel() {
		return btnCancel;
	}

	/**
	 * @param btnCancel the btnCancel to set
	 */
	public void setBtnCancel(JButton btnCancel) {
		this.btnCancel = btnCancel;
	}

	JButton btnCancel = new JButton("Cancel");
	/**
	 * @return the txtRentalNumber
	 */
	public JTextField getTxtRentalNumber() {
		return txtRentalNumber;
	}

	/**
	 * @param txtRentalNumber the txtRentalNumber to set
	 */
	public void setTxtRentalNumber(JTextField txtRentalNumber) {
		this.txtRentalNumber = txtRentalNumber;
	}

	/**
	 * @return the rentalQuantity
	 */
	public JTextField getRentalQuantity() {
		return RentalQuantity;
	}

	/**
	 * @param rentalQuantity the rentalQuantity to set
	 */
	public void setRentalQuantity(JTextField rentalQuantity) {
		RentalQuantity = rentalQuantity;
	}

	/**
	 * @return the txtRentalDate
	 */
	public JTextField getTxtRentalDate() {
		return txtRentalDate;
	}

	/**
	 * @param txtRentalDate the txtRentalDate to set
	 */
	public void setTxtRentalDate(JTextField txtRentalDate) {
		this.txtRentalDate = txtRentalDate;
	}

	/**
	 * @return the txtrentalStatus
	 */
	public JTextField getTxtrentalStatus() {
		return txtrentalStatus;
	}

	/**
	 * @param txtrentalStatus the txtrentalStatus to set
	 */
	public void setTxtrentalStatus(JTextField txtrentalStatus) {
		this.txtrentalStatus = txtrentalStatus;
	}

	/**
	 * @return the cmbEmployeeNumber
	 */
	public JComboBox getCmbEmployeeNumber() {
		return cmbEmployeeNumber;
	}

	/**
	 * @param cmbEmployeeNumber the cmbEmployeeNumber to set
	 */
	public void setCmbEmployeeNumber(JComboBox cmbEmployeeNumber) {
		this.cmbEmployeeNumber = cmbEmployeeNumber;
	}

	/**
	 * @return the cmbCustomerNumber
	 */
	public JComboBox getCmbCustomerNumber() {
		return cmbCustomerNumber;
	}

	/**
	 * @param cmbCustomerNumber the cmbCustomerNumber to set
	 */
	public void setCmbCustomerNumber(JComboBox cmbCustomerNumber) {
		this.cmbCustomerNumber = cmbCustomerNumber;
	}

	/**
	 * @return the cmbVideoNumber
	 */
	public JComboBox getCmbVideoNumber() {
		return cmbVideoNumber;
	}

	/**
	 * @param cmbVideoNumber the cmbVideoNumber to set
	 */
	public void setCmbVideoNumber(JComboBox cmbVideoNumber) {
		this.cmbVideoNumber = cmbVideoNumber;
	}

	/**
	 * Launch the application.
	 */
	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

Java
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) {
				// TODO Auto-generated method stub
				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<>();
		//int videoCopyNumber = Integer.parseInt(view.getCmbVideoNumber().getSelectedItem().toString());
		//int customerNumber  = Integer.parseInt(view.getCmbCustomerNumber().getSelectedItem().toString());
		//int employeeNumber = Integer.parseInt(view.getCmbEmployeeNumber().getSelectedItem().toString());
		
		models.add(model);
		clientManager.sendRent(models);
	}
}


And finally this is the Class that puts all together
Java
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
Posted
Comments
pasztorpisti 6-Aug-13 17:15pm    
Your question is not quite specific and it seems you expect someone to write here an MVC tutorial for you. If your question can be answered only in a book or a long article then its less likely that you get a "good" answer.
There are several good MVC tutorials so I won't write another one here but I have an advice that may come handy: If it isn't necessary then don't separate the view and the controller, in many cases it is much easier and practical to keep them together in the UI. In my opinion separating the view and the controller just increases the cost of maintenance by giving no advantages in many kind of applications. The model part is quite well separated in the java swing framework from the other two parts and that separation is enough to split the gui related view/control code from the logic and the data with ease.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900