• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to reference a component?  RSS feed

 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am working on a GUI. This Gui is constitued of several components. Three of those components are 1 JTable, 2 TextArea.
Each time the user select a row, a message must be displayed in the 2 Textareas. The two TextAreas are in different Panels.
One way I'm using to reference those textareas, is to pass them to the constructor of the ListSelectionListener. But it seems not to be the proper way to do it. Since I have to carry those reference until the Listener constructor. And If I add another component that should be notified, I will have to modify quite a large part of the code to achieve it. So I wanted to know if it is possible that a component is "aware" of the component around him. Or If it is possible to get a reference to whatever component without passing it to a method or a constructor.
Thx for your help
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
as I understand it, you have some information that is displayed all over the GUI and that can be changed by the user. I am sure this information can be wrapped in an Object. Whenever the user changes it, for example by selecting another table row, this object gets changed. Thus, any other components interested in the change, have to be notified by this object. If the GUI is changed, this object can still be used.
In fact, this object is a data model for the view just like the TableModel is for JTable. You can apply the Model-View-Pattern in quite a lot of different cases!
To write your own Listeners see:
http://www.artima.com/designtechniques/eventgen.html
chantal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!