-1

I add an icon to my JMenu which is in an applet. When I run the applet the icon appear allright. But when access it through a browser, as this applet loads in a portlet in a web application, the icon shifts to right in the JMenu.

I do the following for my JMenus. In initilize():

    setOpaque(false);
    setUI((MenuItemUI) BasicMenuUI.createUI(this));
    setContentAreaFilled(false);             
    setFocusPainted(false);
    setBorderPainted(false);
    setFont(UXStandards.MENU_TEXT_FONT);
    setForeground(UXStandards.MENU_TEXT_COLOR);     
    setLayout(new FlowLayout(FlowLayout.LEFT));
    setPreferredSize(new Dimension(80,70));

Then:

trnAnalysisImg = new   ImageIcon(ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("images/MainNav_Btn_TrnAnalysis.png")));

trnAnalysisPressedImg = new ImageIcon(ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("images/MainNav_Btn_TrnAnalysis_Pressed.png")));

terrainAnalysisMenu.setIcon(trnAnalysisImg);
terrainAnalysisMenu.setHorizontalAlignment(SwingConstants.CENTER );**//tried LEFT and RIGHT too, RIGHT works when I run applet but in browser there is no effect**

menubar.setLayout(new BoxLayout(menubar, 
            BoxLayout.LINE_AXIS)); 
menubar.add(fileMenu);
menubar.add(new RGWSeparator(SwingConstants.VERTICAL)); 
menubar.add(terrainAnalysisMenu);

terrainAnalysisMenu.addMenuListener(new MenuListener() {

        @Override
        public void menuSelected(MenuEvent e) {
            RGWMenu menu = (RGWMenu) e.getSource();
            menu.setIcon(trnAnalysisPressedImg);
        }

        @Override
        public void menuDeselected(MenuEvent e) {
            RGWMenu menu = (RGWMenu) e.getSource();
            menu.setIcon(trnAnalysisImg);
        }

        @Override
        public void menuCanceled(MenuEvent e) {
            RGWMenu menu = (RGWMenu) e.getSource();
            menu.setIcon(trnAnalysisImg);
        }       
    });

Here is the outcome: Notice the shifted icon on the menu, which is revealing the background color

Shariq
  • 25
  • 2
  • 8
  • 1
    `setPreferredSize(new Dimension(80,70));` 1) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 12 '16 at 06:39
  • I tried to use setSize(), The icon now leaves equal spaces from both ends of the menu, although both menu and icon are of equal size. – Shariq May 12 '16 at 09:39
  • I'll think more on it once I've seen the MCVE. (I don't have the first clue why you're using it, but the MCVE should explain.) – Andrew Thompson May 12 '16 at 09:41
  • I don't know what else do you need to reproduce/verify this, I have provided the initialization of a JMenu, the layout of its container(Menubar), The things(an icon) this menu contains and the layout it follows.. I am asking If there is something wrong with these. As per your suggestion I tried setSize() instead of setPrefferedSize(), the icon now leaves equal spaces from both ends of the menu, although both menu and icon are of equal size – Shariq May 12 '16 at 09:47
  • *"I don't know what else do you need to reproduce/verify this,"* So ask, rather than ignore the suggestion. – Andrew Thompson May 12 '16 at 09:48
  • What else do you need? – Shariq May 12 '16 at 09:54
  • 1
    An MCVE needs to be C, V & an E as well as M. Uncompilable code snippets are to in any way **complete** & therefore not **verifiable** or an **example** of the problem. – Andrew Thompson May 12 '16 at 09:56
  • Since you are laying so much stress on MCVE.....I get what MCVE is and all the questions might not require to adhere to MCVE, so although i have provided an M and C(a structured question with code), one can get the problem and might not need to compile and reproduce it. In short I was hoping some expert might have pointed out some error in one of the parameters that I have used that might have fixed the UI issue that I am facing. – Shariq May 12 '16 at 10:12
  • *"I was hoping.."* If wishes were horses, no man would walk. Good luck with it. – Andrew Thompson May 12 '16 at 11:41

1 Answers1

0

Adding this solved my problem at least:

terrainAnalysisMenu.setIconTextGap(0);

Though I had a menu with no text, still it needs this.

Shariq
  • 25
  • 2
  • 8