Win a copy of Head First Android this week in the Android forum!

Geoff Berl

+ Follow
since Apr 13, 2011
Geoff likes ...
Netbeans IDE MySQL Database Java
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Geoff Berl

Sadly, I did not think of that, I often over think my work. I am my own worst enemy. I will try this and see how it works out, if it doesn't, as you said, I can modify it later. Thank you.
I am trying to draw up a UML of a Gym membership program. I need Staff Members Gym Members and Yoga Members. If Yoga Members and Gym Members have monthly dues but Staff Members do not, how can I have a Staff Member that is also a Gym Member (meaning they have monthly dues now)? Or would this be a case when I would need separate classes? I was thinking I should create an interface where any member may implement PayingMember.


Paul Clapham wrote:I'd suggest that you are indulging in what's known as "premature optimization" in the computer business.

Shoot, sorry, I do this a lot. Since I do this a lot you'd think I'd be able to tell that I am doing it but I don't. You make a very good point, I will go ahead and code away, ask questions later.
I'm working on a project that is going to query a DB for exercises. The way it's currently going to be laid out is that you will come to a main window that has selection buttons for which type of exercise you want. Then that will bring up a list of exercises in that category. Finally, once a selection is made from the list it will bring up a detailed window showing an image (actually an animation) and text detailing how to perform the exercise. The user will be able to create new exercises if desired but the DB will be loaded with some standard exercises.

Anyway, I'm wondering if there is a best practice for something like this. I am not sure whether I should create all of the exercise objects first and store them in a list where the GUI can query them or if I should query them directly from the database and create a new object each time an exercise is selected.

I would think you would want to query the DB so it's less of a hassle to update the lists when a new exercise is added or an existing is edited. But at the same time I feel that may be wasted processing constantly querying the DB whereas I could just query the database in one big chunk and create all of the objects at once. Then if they edit or create an exercise I'll just append it to or edit the current list.

So, to sum it up, should I preload objects from a database, create new objects each time or both?

Kemal Sokolovic wrote:Please read CarefullyChooseOneForum. This is a topic for the Swing subforum.

Good luck with the question!

Sorry, a few times I've posted in other forums I've been told to post in the beginner forum. I guess I'm not sure where the line is between beginner or a specific topic yet.
9 years ago
Actually, I just made a step forward. While snooping around in the API I found that Nimbus uses SynthComboBoxUI which extends BasicComboBoxUI. That being said, I added


Now it actually shows the full tree in the drop down (Before it was only showing one line, it wasn't adjusting the combobox size to the size of the JPopup) but my JTreeComboBox looks like a JLabel. Somehow my popup messed up that portion of the display, I'm not sure what element that is. I'll keep working on it.
9 years ago
I am borrowing some code I found on the web that shows how to create a combobox with an embedded JTree. I am having an issue because I would like to use the Nimbus LAF but currently the code only has a custom class to handle the Metal, Windows and Motif LAFs. Is there any way to add code to do this for the Nimbus LAF?

I did some searching on the web but I keep coming back with results showing how to change the colors and fonts for Nimbus.

Here is the method overriding the updateUI() of the JComboBox

And here is the custom LAF classes to return a custom PopUp

Here is a link to the actual site with the source code: JTreeComboBox
9 years ago

Tyler Kenna wrote:Best way to remedy this: Observer and Observable. Observable has two methods called setChanged() and notifyObservers(). If your model is now observable, it doesn't care what views are watching it. It just calls notifyObservers(). Now in your driver class or your controller, you can call model.addObserver(view) which will make your view class (which has implemented Observer) "listen" to the Observable model. When it hears model call notfiyObservers(), it then runs its own update() method (required by Observer) and carries out whatever code you have placed in it. That way, you can create both classes without either one having to know about the other.

Thanks for the info, but I think what you explained is exactly what I'm doing. In the main driver class I create a model and view and then I add that view to the model as an "Observer", if you look at my model class you will see that there is an addKarateTermView() method which allows me to add an interface allowing me to make multiple views which all share the same refresh() method. It's like using Observer and Observable but I haven't really dealt with those classes specifically so I made my own methods. The model has an updateModel() method which would be equivalent to the setChanged() and when updateModel() is called in the model it calls an updateViews() method which would be equivalent to notifyObservers().

I took this model from here but I have also seen it implemented in the Head First Design Patterns book in an example where a company has a weather meter spitting out data that it wants displayed, they go on to show you how to do this creating your own methods.. They discuss this pattern just before they introduce the Observer and Observable features stating that basically we didn't need to create those methods since java has the utility built in. Now, it does say in the book that their implementation is a bad one because their views are concrete and thus making the code too tightly coupled but I thought that's what using the interface would do to help. I just chose to do it this way to get a better understanding of how it works. This is a picture from a webpage which also depicts the specific MVC model I chose to use. There is a description of what the numbers mean but this shows you the idea.

Basically, my model doesn't know about the views it just has an array of observers which get updated with the model's data. And my view is not aware of the model, it just knows that it will be getting some data through an inherited refresh() method so that it can update its fields. When my view performs an action, the controller sees it and notifies the model if necessary and possibly updates all of the views.

But, all that being said, I will try to work on implementing the Observer and Observable features since they would decouple my objects even further eliminating the need for the model to be aware of the specific interface(s).

What I am still trying to figure out though, is what data am I supposed to send my views in the refresh method? In the first link I provided from they just have a very simple model and view where the view only shows four stings of data and the refresh method sends the four strings. The Head First book showed an example that used three strings. I'm dealing with a quiz that could have 100+ questions and a variable number of choices, should I have the method break down each question and just send something like refresh(String question, String[] choices)? This way the view wouldn't have access to the actual Quiz object. I'm guessing that is the key of the view, not only should it not be aware of the model but it shouldn't be passed any objects otherwise then the object could be manipulated by the view. Is this the right mindset?
9 years ago

Winston Gutkowski wrote:Well, a Quiz sounds to me like data, which would put it in the M portion, so it shouldn't be seen by the V at all. How you communicate it to the C component is another matter altogether. Question: Is it likely to change?

Remember: V communicates with C; M communicates with C. V should NOT communicate with M directly.


Yes Quiz is data which is created and stored in the Model but any time that changes the model is sending it to the view(s). Should I not send objects to the view?

I wanted to build an MVC with the idea that it will change. I would like to add more quizzes and different views.

I have M communicating directly with V but definitely not the other way around.
9 years ago

Winston Gutkowski wrote:If you get it right, you should be able to write your V component as a bunch of Scanner and System.out.println() statements to prove that the concept and application logic work before you even start work on your GUI. That part may involve refining the messages between C and V, but should almost never affect the M part.

Interesting, I hadn't even thought of using the command line as a "V". That would make it much less confusing to me since I find it slightly overwhelming trying to create all three at once. I'm going to do that.

Since my previous post, I have created completely new Quiz, Question and Choice objects and created a method that generates a quiz. I tested and it works great but now what would I do with the quiz, should I send the entire quiz to the view or just each question? I guess I just don't know how much I should send to the view at one time. If I send the Quiz, then the view would have to parse the quiz which I would guess is "logic" and therefore shouldn't be done in the view. So should I send the question? But if I send the question, the view will need to parse the choices and again, that seems like "logic". So how primitive should I go when sending data to the view?
9 years ago
I think I am starting to get the idea of how to implement an MVC pattern, I just have a few questions on the specific details for my current project. Currently I'm working on a project that is a simple single frame (that will change later) quiz GUI to quiz a user on Karate terms. I have a Model, View and Controller and I also have QuizQuestion and KarateTerm Objects.

My model is gathering a list of KarateTerms from a text file and then I want it to generate a random question with random answers along with the correct answer. I'm getting a little confused as to how to break the processes up.

I guess my first roadblock is which logic goes where....
So my model creates a question and updates the view, then the user makes a selection, the controller sees it and sends that selected answer to the model. Would the model then check if the answer is correct or should that logic be in the Question Object?

Here is the entire code so far, if anyone feels ambitious enough to check it over can you tell me what your initial thoughts are on my structure?
I know my View is very simple and ugly but I only created it to test my model and I see there are flaws with it but I'm not sure how to proceed.

9 years ago
My apologies, this was supposed to be a reply to my original thread "A quick question on MVC (DAO)" Somehow it ended up as a completely new topic. Can this be joined or is it completely messed up now?
Thanks for the clarification, now that I think of it I'm not sure why I asked if the Model set the view's data. The intent behind MVC is to be able to reuse the model for any view which obviously wouldn't work if I programmed it to set component values in any particular view.

Yeah, the reason I say I've been working on understanding it for the past few days is because there are so many different variations, every time I tried to implement one, I would run into an issue and google it only to end up finding something somewhere that contradicted the original paradigm I was going with. I ended up figuring out that no one in particular was right but I chose to go with one where the model and view can only communicate via the controller, there is no path from view to model directly in either direction. To me this seems the easiest to start with, then I may venture into the ones that have the view call to the model for updates as I've seen in some implementations.

I do have one more question if you don't mind...

During the initial start up of the application I want my controller to populate the combobox in my view. Normally my controller get's the combobox component via the listener. How "should" my controller access this component when it isn't event driven? Currently I have created a method in the view like this

I didn't copy that directly so it may not be syntactically correct but you get the picture.

Is this a typical way to perform an action like this? I've also seen something like this
in which case the controller would get the JComboBox and work with it in the controller but that doesn't seem right.

In that case why wouldn't you just make jcbEmployees public, which also doesn't seem right?
I've been working on understanding MVC DAO for the past few days. One thing I'm still not sure of though is what part actually sets the data in my view?

I have a controller which is connected with a model and view
I also have a DAO that the model uses to get data from MySQL but I don't think that's relevant in this question.

When I want to add or update a list in a combo box, say initially or when a listener is triggered...
Does my controller call the model for the list and then put that list in the view?
Or does my controller call to the model to updateCombobox in the view?
Or, is it something different entirely.
I'm not sure if that title made any sense but it's the best I could come up with. I have a CellEditor for my table that is a Combobox

And here is Operation:

So, I have my cell showing a combobox when clicked and it shows the operation Number and Description. Once selected I want the Combobox AND the table value to represent only the number. Basically this is just to allow the user to easily see what number correlates to what process step but only during the actual selection. The result should only display the number though.

I've been trying all sorts of different things but I can't seem to get it to work.
9 years ago