java - How to create hover on JLabel to show Menubar? -
how make show menu bar upon user hovered cursor on jlabel? i'm little bit confused mouseevent, etc. here code:
public class menupage extends jframe implements actionlistener { private jlabel fshirts, tshirts, sweater, jeans, shoes, hats, bags; private jbutton btncart, btnexit; private jpanel leftpane, rightpane; private container cont; public menupage() { super ("menu"); cont = getcontentpane(); cont.setlayout (new borderlayout()); fshirts = new jlabel ("formal shirts", swingconstants.center); tshirts = new jlabel ("t - shirts", swingconstants.center); sweater = new jlabel ("sweater", swingconstants.center); jeans = new jlabel ("jeans", swingconstants.center); shoes = new jlabel ("shoes", swingconstants.center); hats = new jlabel ("hats", swingconstants.center); bags = new jlabel ("bags", swingconstants.center); btncart = new jbutton ("cart"); btnexit = new jbutton ("exit"); leftpane = new jpanel(); leftpane.setlayout (new gridlayout (9,1)); leftpane.setborder (new titledborder ("menus")); cont.add (leftpane, borderlayout.west); leftpane.add (fshirts); leftpane.add (tshirts); leftpane.add (sweater); leftpane.add (jeans); leftpane.add (shoes); leftpane.add (hats); leftpane.add (bags); leftpane.add (btncart); leftpane.add (btnexit); leftpane.setopaque (true); btncart.addactionlistener(this); btnexit.addactionlistener(this); pack(); setlocationrelativeto(null); setvisible(true); setdefaultcloseoperation(exit_on_close); validate(); } public void actionperformed(actionevent menu) { { if (menu.getsource() == btncart) { dispose(); } else if (menu.getsource() == btnexit) { dispose(); new welcomepage(); } } } public static void main(string args[]) { menupage wp = new menupage(); } }
how make label show menu bar when user hovered cursor on each of label?
simply implement mouselistener
(or mouseadapter
, handy) :
fshirts.addmouselistener(new mouseadapter() { @override public void mouseexited(mouseevent e) { system.out.println("hide menu"); } @override public void mouseentered(mouseevent e) { system.out.println("show menu"); } });
by way, code structure cleaner (unless actually need reference on labels, doubt) :
public class menupage extends jframe implements actionlistener { private jbutton btncart, btnexit; private jpanel leftpane, rightpane; private container cont; private static final list<string> labels_text = arrays.aslist("formal shirts","t - shirts", // "sweater","jeans","hats","bags"); public menupage() { super("menu"); cont = getcontentpane(); cont.setlayout(new borderlayout()); leftpane = new jpanel(); leftpane.setlayout(new gridlayout(9,1)); leftpane.setborder(new titledborder("menus")); list<jpopupmenu> popmenus = buildpopmenus(); (int i=0 ; i<labels_text.size() ; i++) { final int j = i; jlabel label = new jlabel(labels_text.get(i),swingconstants.center); label.addmouselistener(new mouseadapter() { @override public void mouseexited(mouseevent e) { popmenus.get(j).setvisible(false); } @override public void mouseentered(mouseevent e) { point loc = label.getlocationonscreen(); loc.translate(label.getwidth(),label.getheight()); popmenus.get(j).setlocation(loc); popmenus.get(j).setvisible(true); } }); leftpane.add(label); } btncart = new jbutton("cart"); btnexit = new jbutton("exit"); cont.add(leftpane,borderlayout.west); leftpane.setopaque(true); btncart.addactionlistener(this); btnexit.addactionlistener(this); pack(); setlocationrelativeto(null); setvisible(true); setdefaultcloseoperation(exit_on_close); validate(); } private list<jpopupmenu> buildpopmenus() { return labels_text.stream().map(text -> { jpopupmenu popmenu = new jpopupmenu(); popmenu.add(new jmenuitem(text)); return popmenu; }) .collect(collectors.tolist()); } public void actionperformed(actionevent menu) { if (menu.getsource() == btncart) { dispose(); } else if (menu.getsource() == btnexit) { dispose(); // new welcomepage(); } } public static void main(string args[]) { menupage wp = new menupage(); } }
Comments
Post a Comment