multithreading - Implementing MVC paradigm in Swing (Java) using Threads -
i trying implement mvc paradigm in java (swing) using multithreading. currently, extending view class observer class , and model class observable class , working fine (my code bulit along example: http://austintek.com/mvc/). controller class calls corresponding model , view functions , there main "glue" program sets everything. however, approach not utilize threads. is there way implement using threads and observer/observable class @ same time?
(my aim implement each of m, v , c separate thread)
the following part of view code:
public class view implements observer { /*************************************** view ************************************* ** ** function constructor of view class. ** ** pre: <nothing> ** post: gui created , directory displayed on screen. ** return: <n/a> ** **/ view(string name) { threadname = name; //frame in constructor , not attribute doesn't need visible whole class jframe frame = new jframe("simple mvc"); frame.setdefaultcloseoperation(jframe.exit_on_close); addelements(frame); // implemented separately // frame.addwindowlistener(new closelistener()); frame.setsize(900, 732); // frame.setlocation(100,100); /** display window **/ //frame.pack(); frame.setvisible(true); }// view
and following part of model code:
public class model extends java.util.observable { /****************** variable declarations *********************/ // hash table in directory stored private hashtable<string, integer> tel_dir = new hashtable<string, integer>(); // path of telephone directory during runtime private url filepath = model.class.getclassloader().getresource("directory.txt"); // name of thread private string threadname; /****** private thread t; ******/ // object in message sent view private message message = new message(); /** getters , setters **/ .... /*************************************** model ************************************* ** ** function constructor of model class. ** ** pre: <nothing> ** post: telephone directory input file , stored in hash ** table. ** return: <n/a> ** **/ model(string name) { int phone_num; string customer_name; scanner scanner = null; threadname = name; /** opening handle file containing telephone directory **/ try { scanner = new scanner(new file(filepath.getpath())); } catch (filenotfoundexception e1) { e1.printstacktrace(); } /** inputting file , storing in hash table **/ while (scanner.hasnextint()) { phone_num = scanner.nextint(); customer_name = scanner.nextline(); customer_name = customer_name.trim(); tel_dir.put(customer_name, phone_num); } }// model
my aim implement each of m, v , c separate thread.
this not useful division. swing views must constructed , manipulated only on event dispatch thread, , users expect swing controls remain responsive, discussed here.
instead, run time-consuming model in background of swingworker
, , update listening views in implementation of process()
. in example, view components updated directly, can fire custom event registered listeners, suggested here. in example, chart
listens data model, series
, via serieschangeevent
; responds worker's update in process()
. can show progress via propertychangeevent
, suggested here or here.
Comments
Post a Comment