Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Have object notify main object  RSS feed

 
Montana Burr
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I want to do is have a label that is updated whenever an object gets some new, relevant data.

The way you do it in Java looks different from the way we do it in Objective-C. In Objective-C, we have what's known as a protocol. An Objective-C protocol is almost exactly like a Java "implementation." In Obj-C, if I want the user to see the address of where he is, I can have an object that gets the information and invokes a view controller's method; at that point, the view controller would then take the data passed to it and display the data in a label.

However, the view controller is an instance of a subclass of the bundled view controller class. In Java, I feel like creating a subclass of JFrame just to accomplish what I set out to accomplish isn't a very good idea.

I, therefore, ask for your help. Thank you!
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Montana Burr wrote:In Java, I feel like creating a subclass of JFrame just to accomplish what I set out to accomplish isn't a very good idea.


Yes, your intuition about GUI design is absolutely correct. You shouldn't create a subclass of JFrame to be your controller. Unfortunately you'll see a lot of bad Swing code which does exactly that, including code which is generated by various widely-known Java tools.

So yes, you should create a separate class which acts as the controller. That code should contain a reference to the JFrame, or at least to the GUI components which it controls, but it shouldn't BE a JFrame.
 
Montana Burr
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Montana Burr wrote:In Java, I feel like creating a subclass of JFrame just to accomplish what I set out to accomplish isn't a very good idea.


Yes, your intuition about GUI design is absolutely correct. You shouldn't create a subclass of JFrame to be your controller. Unfortunately you'll see a lot of bad Swing code which does exactly that, including code which is generated by various widely-known Java tools.

So yes, you should create a separate class which acts as the controller. That code should contain a reference to the JFrame, or at least to the GUI components which it controls, but it shouldn't BE a JFrame.


Thanks for the help!

Just to be clear, that means multiple classes: the one that has the main(), and another one that controls the JFrame.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could be. I usually use a shortcut in which the class which contains the main() method creates an instance of itself which acts as the controller, but if you're a pickier programmer than I am, the class with the main() method could create an instance of a different class.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nowadays, I find myself leaning more and more towards the event bus, i.e. decoupled messaging. I encountered it first while working with GWT and loved it tremendously. There are quite a few frameworks available. Depending on your requirement you can choose using this informative piece http://codeblock.engio.net/37/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!