java - How to externalize a MouseListener? -
(note: aware of existence of mouseadapter
-class, since overriding methods later on, advantage of lost?)
i have class mainprogram.java in i'm adding several components. of them have listener (actionlistener, mouselistener, ...), get's bit crowded in main class.
therefore trying "externalize" listeners own classes. far have used inner classes in main-class, makes accessing variables, components, ... pretty easy , straightforward.
but external listeners not sure best way implement them.
for example, when want find out, of lables has been clicked, getting event, source of event, cast jlabel
, text on label compare string!
this works, seems prone errors (what if change jlabel-text in main-class? -> listener breaks) , pretty unclean.
i've tried search via google , on stackoverflow better ways this, found inner class approach.
so there better way access components / externalize listeners?
public class mainprogram extends jframe { public mainprogram() { super("landing page"); setsize(400, 400); setdefaultcloseoperation(jframe.exit_on_close); jpanel westpanel = new jpanel(); jlabel lbl_orderstatus = new jlabel("order status"); jlabel lbl_technicaldocu = new jlabel("technical documentation"); jlabel lbl_checkreport = new jlabel("check report"); mouselistenerboldfont mouselistenerlabelboldplain = new mouselistenerboldfont(); lbl_orderstatus.addmouselistener(mouselistenerlabelboldplain); lbl_technicaldocu.addmouselistener(mouselistenerlabelboldplain); lbl_checkreport.addmouselistener(mouselistenerlabelboldplain); westpanel.add(lbl_orderstatus); westpanel.add(lbl_technicaldocu); westpanel.add(lbl_checkreport); add(westpanel); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); mainprogram window = new mainprogram(); window.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); }
the mouselistenerboldfont.java:
public class mouselistenerboldfont implements mouselistener{ object lbl_westpanel; @override public void mouseclicked(mouseevent e) { if(((jlabel)e.getsource()).gettext().equals("order status")){ system.out.println("order status clicked"); }; if(((jlabel)e.getsource()).gettext().equals("technical documentation")){ system.out.println("technical documentation clicked"); }; if(((jlabel)e.getsource()).gettext().equals("check report")){ system.out.println("check report clicked"); }; } @override public void mouseentered(mouseevent e) { lbl_westpanel = e.getsource(); ((jcomponent) lbl_westpanel).setfont(new font("tahoma", font.bold, 12)); } @override public void mouseexited(mouseevent e) { lbl_westpanel = e.getsource(); ((jcomponent) lbl_westpanel).setfont(new font("tahoma", font.plain, 11)); } @override public void mousepressed(mouseevent e) { } @override public void mousereleased(mouseevent e) { } }
sure not best way may useful problem
public class mylabel extends jlabel implements mouselistener { public mylabel() { addmouselistener(this); } public mylabel(string txt) { super(txt); addmouselistener(this); } public void mouseclicked(mouseevent e) { system.out.println(gettext() + " clicked"); } public void mouseentered(mouseevent e) { setfont(new font("tahoma", font.bold, 12)); } public void mouseexited(mouseevent e) { setfont(new font("tahoma", font.plain, 11)); } public void mousepressed(mouseevent e) { } public void mousereleased(mouseevent e) { } }
then
jlabel lbl_orderstatus = new mylabel("order status"); jlabel lbl_technicaldocu = new mylabel("technical documentation"); jlabel lbl_checkreport = new mylabel("check report"); // mouselistenerboldfont mouselistenerlabelboldplain = new mouselistenerboldfont(); // // lbl_orderstatus.addmouselistener(mouselistenerlabelboldplain); // lbl_technicaldocu.addmouselistener(mouselistenerlabelboldplain); // lbl_checkreport.addmouselistener(mouselistenerlabelboldplain);
Comments
Post a Comment