aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes mvc again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "mvc again" Watch "mvc again" New topic
Author

mvc again

Alex Gregory
Ranch Hand

Joined: Jun 15, 2002
Posts: 46
Hi, can someone tell me the fault in this logic cause Java gives me runtime exceptions all over the place:
class Model extends Observable implements TableModel{
.....
}
class MyTable extends JTable implements Observer{
......
}
Then I create my table like this:
Model m = new Model();
JTable t = new MyTable(m);
m.addObserver(t);
So when a user presses search, for example, the controller notifies the model and the model notifies the JTable. However, in update(Observerable o, Object oj) in MyTable I put super.repaint() so that the JTable will repaint. Does all this smell right or am I off the track? Thanks a lot for reading this huge post, i tried to keep it as simple as possible.
Eduard Jodas
Ranch Hand

Joined: May 14, 2002
Posts: 80
I don't know why those exceptions, but I don't understand your design either.
JTable and TableModel already implement a sort of Observable-Observer pattern, though with another name. The JTable registers itself automatically to the TableModel, so that the JTable is notified when changes occur in the TableModel. Furthermore, the JTable repaints itself automatically when it is notified of a change.
Alex Gregory
Ranch Hand

Joined: Jun 15, 2002
Posts: 46
ok, well can you tell me how the JTable is notified of a change so that it would repaint itself?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

ok, well can you tell me how the JTable is notified of a change so that it would repaint itself?

Have a method in your table model that you can call when table data changes:

Eugene.
Alex Gregory
Ranch Hand

Joined: Jun 15, 2002
Posts: 46
Thanks guys, that was very helpful. Now another question if you dont mind. My JTable doesnt display its column names even though I implemented the getColumnName method in abstractTableModel. How could that be?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

My JTable doesnt display its column names even though I implemented the getColumnName method in abstractTableModel. How could that be?

That's probably because you didn't put your table in the JScrollPane.
Eugene.
Alex Gregory
Ranch Hand

Joined: Jun 15, 2002
Posts: 46
Eugene, you're the man! I never thought not putting it in the JScrollPane would cause it to fail drawing the column names. Thank you so much.
Alex Gregory
Ranch Hand

Joined: Jun 15, 2002
Posts: 46
Well, one more question for today:
How can I make my JTable such that the user can scroll left and right.
Doing setPrefferedScrollableViewSize didnt do the trick. anyone know of any other way?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6


In the constructor of the JScrollPane you use the Constants of the class to tell it which Scrollbars you need.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Or simply

scrollPaneForTable.setAutoscrolls(true);

Eugene.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mvc again