1

I'm resizing a JPopupMenu and while I can resize font and menu item size just fine, I'm unable to resize other menu things. E.g. shortcut labels or arrows to expand the menu remain small; as can be seen here:

enter image description here

e.g., "Ctrl+C" label (or the arrow next to "Font") has the same size in both menus even though I'd want it bigger in the zoomed menu.

How could I accomplish this?

This is how I resize font and menu item size

//aCollection is a collection with menu items (separators, jmenuitems, etc.) which i want to resize
//aJContainer is a container (JPopupMenu in this case) where I'm adding the resized components
private void addCollectionToJContainer(JComponent aJContainer, Collection aCollection){
    double WEBSTART_ZOOM_FACTOR = 2.5;
    Iterator i = aCollection.iterator();  

    while (i.hasNext()) {
        JComponent item = (JComponent) i.next();    
        try{        
            Font f = ((JMenuItem) item).getFont();
            ((JMenuItem) item).setFont(new Font(f.getName(), f.getStyle(), (int) Math.round(f.getSize() * WEBSTART_ZOOM_FACTOR)));
            int w = (int) Math.round(item.getPreferredSize().getWidth() * WEBSTART_ZOOM_FACTOR);
            int h = (int) Math.round(item.getPreferredSize().getHeight() * WEBSTART_ZOOM_FACTOR);
            item.setPreferredSize(new Dimension(w, h));        
        }catch(ClassCastException e){  //item is a separator
        }
        aJContainer.add(item);
    }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
Václav Pruner
  • 310
  • 1
  • 11
  • 1
    1. change Font, 2. (you can confortly to) get container - BasicsPopupXxx, 3. call pack(), 4. there is BoxLayout in API – mKorbel Jan 18 '16 at 12:20
  • Don't use a try-catch and then blindly cast you item, catching the error if the cast fails. You should instead just test whether the cast will succeed: `if ( item instanceof JMenuItem ) { // cast and change font }` – FredK Jan 18 '16 at 19:48
  • @mKorbel I do not see how your suggestion works. Can you show? – user1803551 Jan 19 '16 at 23:34
  • @user1803551 for example [getWindowAncestor](http://stackoverflow.com/questions/12801035/how-can-i-make-a-jpopupmenu-transparent/12802101#12802101) and here is [pack()](http://stackoverflow.com/a/5060096/714968) – mKorbel Jan 20 '16 at 08:17
  • @mKorbel The arrow icon is a `MetalIconFactory.MenuArrowIcon`, I don't think that `pack` will enlarge it. – user1803551 Jan 20 '16 at 21:05

0 Answers0