File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mvc again

 
Alex Gregory
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, well can you tell me how the JTable is notified of a change so that it would repaint itself?
 
John Smith
Ranch Hand
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the constructor of the JScrollPane you use the Constants of the class to tell it which Scrollbars you need.
Mark
 
John Smith
Ranch Hand
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or simply

scrollPaneForTable.setAutoscrolls(true);

Eugene.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic