Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessor methods for Swing components

 
Tahir Abbas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

After reading one piece of program on net I find out some people use accessor methods for swing components.

I think swing components already have getter and setter methods. e.g; for JTextField we have setText and getText so why do we need to call these methods from our own written setter and getter methods.

I myself declared these swing components private and directly used provided methods (setText/getText etc).

do you think its okay.

Thanks! in Advance
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tahir,

That sounds weird. The only way this sounds reasonable is as a kind of delegator. Sample code to illustrate:

Otherwise I don't see any benefit of the described approach.

Kind regards,
Roel
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did the same way as my good buddy Roel showed. Also, I created an interface for these methods (such as String getHotelName(), String getHotelLocation() and so on) and had my window implement. And this interface was used when someone (such as an ActionListener) needed any data entered by the user.
 
Tahir Abbas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got something but for implementing properly I need some more help if you can tell any link I'll be very thankful.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tahir,

If you have Andrew's book, you should take a look to the ConfigSettings panel. It uses the same approach if I'm not mistaken.

Kind regards,
Roel
 
Tahir Abbas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! for help.

One thing that I got of using getter and setter is to provide security of data and in future any other programmer can use these methods for extension of program. But this way do I need to provide getter and setter for menu bar(s) and even for JLabel(s), Am I right.


In my program I used cardLayout. Different panels come from different files. If I keep all swing components private then I'll need to call getter and setter for every thing including JPanels, . This way in some files number of lines will increase multiple times.
Best Regards!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic