• 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

Encapsulating JComponent Instance Variable

 
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, my name is Jeffry, I have a little bit problem with my GUI based application design.

Do I need to encapsulate all my JComponent instance variable such as JButton, JTextField, JTextArea, etc and provide setter and getter?

I know the importance about information hiding, but does it make sense to encapsulate all the JComponent instance variable? Please help me with this thing before my application become larger. Thanks before.


Thanks


Jeffry Kristianto Yanuar
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As always, it depends. You probably don't need to provide getters/setters for all of your components. Just take it on a case by case basis. If you have a class that needs access to a member variable of another class then providing a getter for that object might be a good idea.

someClass.getLogoutButton();

If you don't indent to provide any logic in your getter method, some would argue that just making the variable public is good enough

someClass.logoutButton;

Of course, you can always just pass object around

(from someClass)
someOtherClass(logoutButton);

Hope I didn't confuse you more than help. I just think that without more information about your specific situations there are many ways and none are technically wrong.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Gregg, thanks for your help!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gregg Bolinger:
If you don't indent to provide any logic in your getter method, some would argue that just making the variable public is good enough

someClass.logoutButton;


And then someone sets the reference to null, and all code breaks. Way to go!
You really want a getter only, no setter for this.

If you pass the entire object, you do run the risk that calling code changes things you definitely don't want them to. Changing the action / action handlers for buttons, changing the caption, it's all possible if they have full access to the button.

What I usually do is just create methods for all interaction I allow; only if I want to allow the changing of the caption would I create a method "setButtonText(String text)". This way, you as a creator of your code still remain in charge of what happens to the object.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob. What about if I make all GUI components instance variable to default modifier and then provide only getter for some GUI components that accessed outside the package? Is it safe?


Thanks


Jeffry Kristianto Yanuar
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would remove the threat of setting it to null, but it still allows users to modify your components.
If you have no problems with that then that's perfect of course.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That would remove the threat of setting it to null, but it still allows users to modify your components.



What does it mean? can you give me an example? Thanks
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As you can see, the Malicious class can modify the button in such a way that the original code won't work - for example, by removing all action listeners, or adding its own action listener that can do evil nasty things.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I see. That's all I need to know about my problem. Thanks for helping me.


Thanks


Jeffry Kristianto Yanuar
reply
    Bookmark Topic Watch Topic
  • New Topic