java - How can I add to or change a label on a JMenuItem? -


i need add label on right hand side of jmenuitem, labels shown below:

menu items labels on right side

i have application uses ctrl++ , ctrl+- zoom image. however, + key default (without shift) = key. when try adding accelerators these menu items, ctrl+- shortcut label displays "ctrl+minus" (i prefer "ctrl -") , ctrl++ shortcut label displays "ctrl+equals" (even worse - in interest of user experience, prefer "ctrl +"):

menubar_view_zoomin.setaccelerator(keystroke.getkeystroke(keyevent.vk_equals, actionevent.ctrl_mask)); 

enter image description here

i want ctrl++ display "ctrl +" , ctrl+- display "ctrl -". how can done?

not answer, need search

  • paint() heavy, paintcomponent() lightweight jpopup, jmenu (for custom painting can switch isheavyweight...)

  • overlay jlabel container (few question jtable @guillaume polet , @robin)

  • create own jmenu/jpopup (see comment question)

enter image description here

enter image description here

import java.awt.color; import java.awt.component; import java.awt.dimension; import java.awt.flowlayout; import java.awt.graphics; import java.awt.insets; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.windowadapter; import java.awt.event.windowevent; import javax.swing.icon; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.jpanel; import javax.swing.swingconstants; import javax.swing.uimanager; import javax.swing.border.etchedborder; import javax.swing.border.titledborder; import javax.swing.plaf.basic.basicarrowbutton;  public class comboboxmenuexample extends jframe {      public comboboxmenuexample() {         super("comboboxmenu example");         string[] itemstr = {"name", "red", "blue", "number", "255,0,0", "0,0,255",             /*separator*/ "system", "control", "controlhighlight", "controlshadow", "text"};         jmenuitem[] menuitems = new jmenuitem[7];         menuitems[0] = new jmenuitem(itemstr[1]);         menuitems[1] = new jmenuitem(itemstr[2]);         menuitems[2] = new jmenuitem(itemstr[4]);         menuitems[3] = new jmenuitem(itemstr[5]);         menuitems[4] = new jmenuitem(itemstr[8]);         menuitems[5] = new jmenuitem(itemstr[9]);         menuitems[6] = new jmenuitem(itemstr[10]);         jmenu[] menus = new jmenu[4];         menus[0] = new jmenu(itemstr[0]);         menus[1] = new jmenu(itemstr[3]);         menus[2] = new jmenu(itemstr[6]);         menus[3] = new jmenu(itemstr[7]);         menus[0].add(menuitems[0]);         menus[0].add(menuitems[1]);         menus[1].add(menuitems[2]);         menus[1].add(menuitems[3]);         menus[3].add(menuitems[4]);         menus[3].add(menuitems[5]);         menus[2].add(menus[3]);         menus[2].add(menuitems[6]);         jmenu menu = combomenubar.createmenu(menuitems[0].gettext());         menu.add(menus[0]);         menu.add(menus[1]);         menu.addseparator();         menu.add(menus[2]);         combomenubar combomenu = new combomenubar(menu);         jcombobox combo = new jcombobox();         combo.additem(itemstr[1]);         combo.additem(itemstr[2]);         combo.additem(itemstr[4]);         combo.additem(itemstr[5]);         combo.additem(itemstr[8]);         combo.additem(itemstr[9]);         combo.additem(itemstr[10]);         getcontentpane().setlayout(new flowlayout());         getcontentpane().add(new combopanel("fake combobox", combomenu));         getcontentpane().add(new combopanel("combobox", combo));     }      class combopanel extends jpanel {          combopanel(string title, jcomponent c) {             setlayout(new flowlayout());             setborder(new titledborder(title));             add(c);         }     }      public static void main(string args[]) {         try {             uimanager.setlookandfeel("com.sun.java.swing.plaf.windows.windowslookandfeel");         } catch (exception evt) {         }         comboboxmenuexample frame = new comboboxmenuexample();         frame.addwindowlistener(new windowadapter() {             @override             public void windowclosing(windowevent e) {                 system.exit(0);             }         });         frame.setsize(370, 100);         frame.setvisible(true);     } }  class combomenubar extends jmenubar {      jmenu menu;     dimension preferredsize;      public combomenubar(jmenu menu) {         this.menu = menu;         color color = uimanager.getcolor("menu.selectionbackground");         uimanager.put("menu.selectionbackground", uimanager.getcolor("menu.background"));         uimanager.put("menu.selectionbackground", color);         menu.updateui();         menuitemlistener listener = new menuitemlistener();         setlistener(menu, listener);         add(menu);     }      class menuitemlistener implements actionlistener {          @override         public void actionperformed(actionevent e) {             jmenuitem item = (jmenuitem) e.getsource();             menu.settext(item.gettext());             menu.requestfocus();         }     }      private void setlistener(jmenuitem item, actionlistener listener) {         if (item instanceof jmenu) {             jmenu menu1 = (jmenu) item;             int n = menu1.getitemcount();             (int = 0; < n; i++) {                 setlistener(menu1.getitem(i), listener);             }         } else if (item != null) { // null means separator             item.addactionlistener(listener);         }     }      public string getselecteditem() {         return menu.gettext();     }      @override     public void setpreferredsize(dimension size) {         preferredsize = size;     }      @override     public dimension getpreferredsize() {         if (preferredsize == null) {             dimension sd = super.getpreferredsize();             dimension menud = getitemsize(menu);             insets margin = menu.getmargin();             dimension retd = new dimension(menud.width, margin.top                     + margin.bottom + menud.height);             menu.setpreferredsize(retd);             preferredsize = retd;         }         return preferredsize;     }      private dimension getitemsize(jmenu menu) {         dimension d = new dimension(0, 0);         int n = menu.getitemcount();         (int = 0; < n; i++) {             dimension itemd;             jmenuitem item = menu.getitem(i);             if (item instanceof jmenu) {                 itemd = getitemsize((jmenu) item);             } else if (item != null) {                 itemd = item.getpreferredsize();             } else {                 itemd = new dimension(0, 0); // separator             }             d.width = math.max(d.width, itemd.width);             d.height = math.max(d.height, itemd.height);         }         return d;     }      public static class combomenu extends jmenu {          arrowicon iconrenderer;          public combomenu(string label) {             super(label);             iconrenderer = new arrowicon(swingconstants.south, true);             setborder(new etchedborder());             seticon(new blankicon(null, 11));             sethorizontaltextposition(jbutton.left);             setfocuspainted(true);         }          @override         public void paintcomponent(graphics g) {             super.paintcomponent(g);             dimension d = this.getpreferredsize();             int x = math.max(0, d.width - iconrenderer.geticonwidth() - 3);             int y = math.max(0,                     (d.height - iconrenderer.geticonheight()) / 2 - 2);             iconrenderer.painticon(this, g, x, y);         }     }      public static jmenu createmenu(string label) {         return new combomenu(label);     } }  class arrowicon implements icon, swingconstants {      private static final int default_size = 11;     //private static final int default_size = 5;     private int size;     private int iconsize;     private int direction;     private boolean isenabled;     private basicarrowbutton iconrenderer;      public arrowicon(int direction, boolean ispressedview) {         this(default_size, direction, ispressedview);     }      public arrowicon(int iconsize, int direction, boolean isenabled) {         this.size = iconsize / 2;         this.iconsize = iconsize;         this.direction = direction;         this.isenabled = isenabled;         iconrenderer = new basicarrowbutton(direction);     }      @override     public void painticon(component c, graphics g, int x, int y) {         iconrenderer.painttriangle(g, x, y, size, direction, isenabled);     }      @override     public int geticonwidth() {         //int retcode;         switch (direction) {             case north:             case south:                 return iconsize;             case east:             case west:                 return size;         }         return iconsize;     }      @override     public int geticonheight() {         switch (direction) {             case north:             case south:                 return size;             case east:             case west:                 return iconsize;         }         return size;     } }  class blankicon implements icon {      private color fillcolor;     private int size;      public blankicon() {         this(null, 11);     }      public blankicon(color color, int size) {         //uimanager.getcolor("control")         //uimanager.getcolor("controlshadow")         fillcolor = color;         this.size = size;     }      @override     public void painticon(component c, graphics g, int x, int y) {         if (fillcolor != null) {             g.setcolor(fillcolor);             g.drawrect(x, y, size - 1, size - 1);         }     }      @override     public int geticonwidth() {         return size;     }      @override     public int geticonheight() {         return size;     } } 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -