• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mvc again

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or simply


scrollPaneForTable.setAutoscrolls(true);


Eugene.
 
knowledge is the difference between drudgery and strategic action -- tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic