Patrick.SE
Patrick.SE

Reputation: 4564

MVC Pattern and JTable's

I've implemented this MVC pattern into my system :

http://www.oracle.com/technetwork/articles/javase/index-142890.html

Everything is good and dandy, I have different views and they talk to the controller when they need to put changes into the model or get called upon when changes are reflected int he model.

Now, I need this new view which uses a JTable. I've decided to extend by model's as AbstractTableModels, now only am I not sure if this is right, but the Java website proposes this to bind the model to the table:

public TableDemo() {
    ...
   JTable table = new JTable(new MyTableModel());
   ...
}

This just feels wrong, I don't have a reference to my model in my view, so how am I supposed to give a model from my view to the JTable? I could create a ;getModel; method in my controller for this special case, but still I don't know if there's a better way to deal with this.

Thanks

Upvotes: 1

Views: 2126

Answers (1)

trashgod
trashgod

Reputation: 205785

Your approach is correct; EnvTableTest is an example. The table's getModel() method will return your TableModel. See also this related answer.

Upvotes: 2

Related Questions