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

Using Controllers to access data  RSS feed

 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good afternoon, I'm trying to wrap my head around how to use controllers to access data in my GUI (rather than linking my data source directly into the GUI).
I have created a small program that details the problem I'm having... I have a method that I use to gather some data and then use my controller to create new channels.  From within that method if I run getChannels against the controller I can see that the objects have been created, but once control passes back to my GUI class if I call the controllers getChannels method it just returns a null value.   Can someone please take a look and point out what I'm doing wrong here?


GUI Creation


Controller


Channel Class


ChannelData


Class that would gather channel data and create new channel objects
 
Campbell Ritchie
Sheriff
Posts: 55295
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use CapitalLetters to start package names.
Why is the method in the GatherChanData (confusing name there) static? What are you doing with its variables, which all appear to be local and to have no existence outwith that method? Why are you using a Boolean[] rather than a boolean[]? Call that field favorite not isFavorite and call the method isFavorite without get. Write yourself a toString method, and you should be able to see from that printout whether you are loading anything at all.
 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback.  

I changed the package names to lowercase and updated the Boolean to be boolean.  (my mistake there, haven't really had to use those up until this point)

The GatherChanData is really just a way for me to encapsulate a fairly complicated method that I use to import a big text file and then load line by line to parse and find the channel data and then create the individual channels.   I kept it as static because I don't really need to  create a GatherChanData object, I just need to gather the data elements from the text file to create the Channel objects.  I did try removing static and instantiating the class before calling the logChanData method and I get the same results.

I haven't created a toString yet but I can see that Channels are being created when I debug,  and in the console messages.   But once I get back to the GUI and run the Controller getChannels method all of the Channels that we created seem to no longer be in scope.   So I'm clearly not understanding how controllers and Data sources work in Java.  




 
Knute Snortum
Sheriff
Posts: 3939
92
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class called Controller is just a data model that holds channel data.  I don't think it's necessary.  Instead, modify GatherChanData to operate directly on an instance of ChannelData and return this instance back to StartGui.

A controller should contain the "business logic" of the application and/or control how the views interact with each other.

Note on package names: The first part(s) of your package name should be something unique.  I own the domain snortum.net, so my packages start with "net.snortum".  If you don't own a domain, try to pick a name that's unique and start with that.  Next should be the application's name, then "model", "view", or "controller", and maybe "app".  So your package names might look like this:

    herronjosh.channelgetter.model.ModelName
    herronjosh.channelgetter.view.ViewName
    herronjosh.channelgetter.controller.ControllerName
    herronjosh.channelgetter.app.Main
   
 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tip on the package names.   In my real app I have more distinct names, but I like your suggestions so I will update everything this evening.

I am starting to understand how to access the data.   It looks like each time I create a new instance of ChannelData all of the channels in the list are wiped out and thus the list that is returned when I call getChannels is null. So I guess the best way to ensure that the class I use to access data across classes/methods remains static and I must not create a new instance.   Is that typically how data access is handled?

For example, once I create a number of new channel objects (in the channel class) I may need to pull a list of existing channels into a handful of other classes and methods.   Right now the only way I know to do this is to create the list of Channel objects in my ChannelData (or even controller) class and then run getChannels.   If there is another way to accomplish this outside of keeping a static instance of ChannelData I would love to hear it.
 
Knute Snortum
Sheriff
Posts: 3939
92
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the best way to implement this kind of thing is with a pattern called lazy instantiation (I believe).  It would look like this:

Note that this doesn't deal with concurrency.  It also assumes that there is one set of channels that doesn't change during the lifetime of the application.
 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again, I'm well on my way to "getting it" now.

Who says you can't teach an old dog new tricks.  :P
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!