Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Encapsulating JComponent Instance Variable  RSS feed

 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • 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
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ok Gregg, thanks for your help!
 
Rob Spoor
Sheriff
Posts: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!