2

I have a separate class that implements a TableModel interface used for JTable. I have one Boolean column, presented as a column of checkboxes and I was wondering how I can inform an external object that certain values need to be updated? If I had this implementation as a nested class, I could access the members of the main class from within, but if I decided to move it to a separate class, do I need to register my main class as a listener on the Table model implementation and then raise an event?

aymeric
  • 3,877
  • 2
  • 28
  • 42
Bober02
  • 15,034
  • 31
  • 92
  • 178
  • 1
    It seems your question already contains the answer. Yes, you should register your main class as a listener on the TableModel, and then raise an event when one of the values is updated. – LordOfThePigs Aug 14 '12 at 09:31
  • from external classes to the JTableM Model??? or from JTable Model out ...??? – mKorbel Aug 14 '12 at 09:51
  • I was thinking about subscribing to tableModel events – Bober02 Aug 14 '12 at 10:21
  • @mKorbel: This [view](http://stackoverflow.com/a/11921532/230513) may be the one intended to listen to the `TableModel`. +1 for preferring loose coupling. – trashgod Aug 14 '12 at 10:27

2 Answers2

4

Expanding on @gontard's correct answer, you can arrange for your view (say a CombinedDomainXYPlot) to listen to your TableModel. Both known subclasses include the required addTableModelListener() implementation. Your view can then react accordingly when it sees the change.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

Your external object should listen directly to the model value change. There is no need to add a dependency on the view.

gontard
  • 28,720
  • 11
  • 94
  • 117