java - How would you center a JMenu's Popup? -


i'm trying center jmenu's popup can use on jpanel , doesn't off. here code demos trying do:

import javax.swing.*;  public class menu extends jmenu{  public static void main(string[] args) {             jframe f = new jframe("menu test");     f.setdefaultcloseoperation(jframe.exit_on_close);      jmenubar menubar = new jmenubar();     menubar.add(new menu());      jpanel background = new jpanel();     background.add(menubar);     f.setcontentpane(background);      f.setsize(250, 100);     f.setlocationrelativeto(null);     f.setvisible(true); }  public menu() {     super("i'm menu");      add(new jmenuitem("can popup centered?"));     add(new jmenuitem("not right?")); } } 

here's current output

here image of output

here's want (or close to)

here (close to) want

if there better way other using jmenu, please let me know. thanks.

i figured out answer. override processmouseevent know when menu clicked, , set location of popup menu relative location of menu.

    @override     protected void processmouseevent(mouseevent e) {         super.processmouseevent(e);         if(e.getid() == mouseevent.mouse_pressed)             getpopupmenu().setlocation(                 getlocationonscreen().x+getwidth()/2-getpopupmenu().getwidth()/2,                 getlocationonscreen().y+getheight()); } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -