MVC in the JTable


The JTable component models the MVC design pattern. In the MVC design pattern, there are three components: a view (the information the user sees), a model (the underlying data that populates the view), and a controller (which typically keeps the model and the view in sync).The Swing architecture uses the MVC design pattern extensively and the JTable is one of the components that rely on this pattern. The visible grid that the user sees and manipulates represents the view. The corresponding data is stored in the model, an object instantiated from a subclass of the AbstractTableModel class. You extend this class to create your own model class as is shown in Listing 15.1. The model is responsible for storing and keeping the data current. Note that the data shown in the view and the data in the model are not automatically kept synchronized. The synchronization between the two must be controlled by the controller, a piece of software that you must write. This is where most of your code goes that affects the view. The controller keeps the view and the model synchronized and can enable the model to be updated based on user input in the view. For example, when he user sets focus to a specific grid cell and presses a Delete button, the controller could respond by deleting the actual data in the model and updating the view as necessary. To see how all this works, let's create a simple JTable-based application.



JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net