swing - Java Menubar Action to a Class -
i have java menubar in program has 1 option 2 sub-options, e.g. file -> save, close. instead of save , close, options server , client. action event 1st option have java action listener:
public class serveraction extends abstractaction { public serveraction() { super(); } public void actionperformed(actionevent e) { joptionpane.showmessagedialog(null, "test"); } }
so works when click on file->server, pops window says test. have server class (that have tested separately , know works) looks this:
public class socketserver { public static void main(string[] args) throws exception { ... } private static class clientlistenthread extends thread { public clientlistenthread(socket socket, int clientnumber){ ... } public void run() { ... } } private static class serversendthread extends thread { public serversendthread(socket socket) { ... } public void run() { ... } } }
now need call socketserver
class when click on server option of main program can start server code , wait , listen client connections. question is, how start entire socketserver
class code serveraction
class?
your nested-classes private
means visible in serversocket
class. can provide helper method more visibility or change class declaration signature more visibility.
example helper method:
public class socketserver { . . //nested classes declaration public static void startserver(){ //code start threads new somethread().start(); } }
and in action
public class serveraction extends abstractaction{ @override public void actionperformed(actionevent e){ socketserver.startserver(); joptionpane.showmessagedialog(null, "test"); } }
it great if can notify when client connected , on, i'd recommend take swingworker
provide helpful tool handle notification between background thread , gui thread.
read more: worker threads , swingworker
note : great if implements runnable
rather extend thread, not adding special functionality thread there no reason, more details here --> "implements runnable" vs. "extends thread".
Comments
Post a Comment