0

I have extended the AbstractTableModel to suit my requirements. Now this table can be altered by other methods of my GUI. I want the table to scroll to the currently edited cell into view. To do this, I think I have to first get the JViewport of the current JComponent, but I see no method by which I can achieve this? How do I achieve this?

I have already done this when I have used the default JTable, but how do I do this when we extend AbstractTableModel?

halfer
  • 19,824
  • 17
  • 99
  • 186
sasidhar
  • 7,523
  • 15
  • 49
  • 75

2 Answers2

1

Models are designed to store data and notify views when the data has changed. It notifies the view of a change in data by firing events. It is the responsibility of the view to listen for these events. Therefore the model never knows directly what view is being updated. This type of functionality should NOT be part of the model.

One approach might be to use a TableModelListener. You can create a TableModelListner with the table as a paramenter. Then when data is changed the listener will be notified. You can then invoke table.scrollRectToVisible(...) on the table. However, with this approach you can't distinquish between edits that have been applied directly through the TableModel versus udates that have been done through the JTable itself.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

You may want to have your table fire an event, and have your parent component listen for that event, and scroll accordingly. That way your table doesn't need to know about its parent scroll pane.

You can make use of the EventListenerList in DefaultTableModel to notify any listeners.

Sam Barnum
  • 10,559
  • 3
  • 54
  • 60
  • I didn't get you clearly, how do we scroll without getting the viewport? And when i extend abstract table model, the hierarchy is something like Object-->AbstractTableModel-->MyTableModel. How do i get the viewport so that i can achieve the scrolling? – sasidhar Dec 28 '10 at 19:35
  • Please help me out, I need it very urgently. – sasidhar Dec 28 '10 at 20:06
  • Which class instantiates your custom table model? Have that component class add a custom listener to your custom table model. When an edit happens in your table model, your table model notifies all listeners. Your listener should have a reference to both the table model and the JScrollPane, and can scroll accordingly. – Sam Barnum Dec 29 '10 at 06:30