Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

(Swing) Model seems like a Controller?

Thread view: 
Joop - 10 Jan 2004 18:24 GMT
Hello,

I'm in the process of making my own ListModel by extending
AbstractListModel. That's not difficult. But i'm having problems with
my infrastructure, my new created Model tend to be more like a
Controller. For example:

====
public class UserManagerListModel extends AbstractListModel {
 private ArrayList listUsers;
 private UserManager um;
 private SessionFactory sessionFactory;

 public UserManagerListModel(SessionFactory sessionFactory) {
   this.sessionFactory=sessionFactory;
   um=new UserManager(sessionFactory);
   listUsers=(ArrayList)um.getUsers();
 }
 public Object getElementAt(int index) {
   return listUsers.get(index);
 }
 public int getSize() {
   return listUsers.size();
 }
 private void updateUsers(){
   listUsers=(ArrayList)um.getUsers();
   this.fireContentsChanged(this, 0, getSize());
 }
 public void storeUser(User u){
   um.storeUser(u);
   updateUsers();
 }
 public void removeUser(User u){
   um.removeUser(u);
   updateUsers();
 }
}
====

The variable "UserManager" is my DAO object which stores and retrieves
and find data in the database.

Additional i have a JFrame which contains the JList with the
"UserManagerListModel" as model.

====
JList listusers = new JList();
listusers.setModel(new UserManagerListModel(sessionFactory));
====

The Frame also consists of several Add, Remove and Change buttons to
change the name of the user. For example a user press the Add button.
The following happens:

===
User u=id.getUser();
u.setGebruikersnaam(naam);
((UserManagerListModel)listusers.getModel()).storeUser(u);
===

As you can see i must get the Model from the JList, do a cast and then
i call the storeUser() methode of the UserManagerListModel to save the
user to the database.

I do not really like the construct:
"((UserManagerListModel)listusers.getModel()).storeUser(u);"
it's seems a bit like dirty programming to me.

Does there exists a better solution (/pattern) with not much overhead,
which makes things a little cleaner?

Thanks,

joop
Karl von Laudermann - 11 Jan 2004 06:10 GMT
> I do not really like the construct:
> "((UserManagerListModel)listusers.getModel()).storeUser(u);"
> it's seems a bit like dirty programming to me.

What I would do is simply hold a reference to the model itself, in addition
to the JList. Then you can write something like:
listmodelusers.storeUser(u);
instead of the ugly construct above.

> Does there exists a better solution (/pattern) with not much overhead,
> which makes things a little cleaner?

Well, you could use the pattern that I've used in the past (for a JTree
rather than a JList, but the principle's the same): Create a subclass of
JList called UserManagerList, which implements methods for managing the
list. UserManagerList stores a reference to the list model in a private
member variable. But instead of subclassing AbstractListModel, just use
a DefaultListModel object.

Signature

Karl von Laudermann - karlvonl(a)rcn.com - http://www.geocities.com/~karlvonl
#!/usr/bin/ruby
c=" .,:;i|+=ahHME8";def l(a,b,c)x=b-a;y=c-a;Math.sqrt(x*x+y*y)end;25.times{|y|
50.times{|x|print(l(12,x/2,y)<=12?((c[l(8,x/2,y).to_i]||36).chr):" ")};puts""}

Karsten Lentzsch - 11 Jan 2004 12:04 GMT
> Hello,
>
> I'm in the process of making my own ListModel by extending
> AbstractListModel. That's not difficult. But i'm having problems with
> my infrastructure, my new created Model tend to be more like a
> Controller.

I'd factor out the control behavior to a true controller
that refers to a List-based ListModel implementation.

I personally use classes ArrayListModel and LinkedListModel
that extend ArrayList and LinkedList and implement the
ListModel interface. So I can operate on the Model with
the full List power and observers get the fine grained
ListDataEvents.

Also, I recommend to use a JList, JComboBox, etc.,
not extend it - unlike there's special UI component behavior.
So I end up using a generic List+ListModel implementation,
use the generic Swing component and a custom controller.

Hope this helps,
Karsten
Joop - 12 Jan 2004 19:28 GMT
>> Hello,
>>
[quoted text clipped - 16 lines]
>So I end up using a generic List+ListModel implementation,
>use the generic Swing component and a custom controller.

Thanks Karsten and Karl for your replies.

That seems like a good solution! I'm going to try this immediately.

Greetings,

joop


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.