Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 238 Big Data Appliance
- 1.9K Data Science
- 450.2K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 154 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 436 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
How to completely separate Model from View in MVC? Or How the Model Updates the View?

Hello Experts,
I'm Building a Swing Application but i'm having a problem trying to implement MVC,
I searched a lot about this but all explanations or examples i found has to be missing something.
Based on my understanding in an MVC environment the View and the Model have to be completely decoupled ... meaning the we shouldn't import any classes from the Model into the View or vice versa (Correct?)
And i did this already but in some situations i find my self in a position that i must import a class from the model into the View.
For example ... The app i'm building is a POS application.
So in the View i have a table ... this table displays the items been sold.
Now when user choose an item he calls a method in the Controller which in turn calls a method in the model to query the database for that item gets all the information and then add it to a List<Product>.
The view then calls another method in the Controller to get that List and update the JTable with the new list (to show the items on the View JTable).
But here is my problem the List ... to show the item's info on the JTable a List is not sufficient instead i need a List<Product> to be able to get each product column value.
And there you go now i must import Product which is POJO i created in the model that holds the product attributes.
I hope i explained it well ... i wish i could support this question with my codes but i'm pretty sure it'll confuse you more than it'll help ... I'm working on it.
Note: I know how to reach the Model From the View (Using Controller methods) ... But what i really can't understand is how the Model updates the view.
Thank you for your time
Gado
Answers
-
Hi, I need more information about your case. But in the mean time, you need to create a custom TableModel.
JTable only show the data, but all operations that you want to perform with the data must be through the TableModel.
Here, a simple example https://github.com/ecabrerar/diplomado-java-pucmm-mescyt/blob/master/programacion-cliente-java/labs/javase_swing_jtable.
Explanation about the example: Sorry it is into Spanish Language
-
Hi eudriscabrera ... Thank you for your response.
I do have an AbstractTableModel for my table.
The problem i'm having is how to get the Product value from the Model to the View (TableModel) without importing any of the Model classes.
Ok let me rephrase the question ... How can i update the View's TableModel after changing the Model?
I know how to reach the Model From the View (Using Controller methods) ... But what i really can't understand is how the Model updates the view.
I'm sorry but i don't know Spanish.
Thank you
Gado
-
I used to use a method to set the data to the TableModel, in this way, I can update the TableModel when the data change.
For Example:
public class TableModelCar extends AbstractTableModelStandard {
public TableModelCar() {
super(new String[]{"Model", "Brand", "Year", "Color"});
}
public Object getValueAt(int rowIndex, int columnIndex) {
......
}
}
}
When you open the window for the first time, you must initialize the TableModel.
private void initTableModel() {
jTable1.setModel(new TableModelCar());
jTable1.setRowSelectionAllowed(true);
if (jTable1.getRowCount() > 1) {
jTable1.setRowSelectionInterval(0, 0);
}
}
If your model change and you must update the TableModel
TableModelCar tmCar = (TableModelCar) jTable1.getModel();
tmCar.setData(list);
jTable1.setModel(tmCar);