Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Have object notify main object  RSS feed

 
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!
 
Marshal
Posts: 24503
55
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
Marshal
Posts: 24503
55
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.
 
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/
 
Why is the word "abbreviation" so long? And this ad is so short?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!