3

I'm using Eclipse Luna with Java 1.7 and am trying to create a JPanel that has a JMenuBar. This JMenuBar contains a JMenu which again contains a JMenuItem. To fit the whole JMenuBar into my project's visual design, I changed the background and foreground colors of JMenu, JMenuBar and JMenuItem using UIManager.put(). The result should be a drop-down menu with sections only divided by different shades of a Color (blue in my example). No lines or borders.

However, a whiteish, silverish etched border remains on the MenuItem.

My question is this: How can I make this border disappear?

I'd also like to know what element this border appears on and if it's a "feature" or there's some kind of sense to it.

SSCCE:

package sscce;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
@SuppressWarnings("serial")
public class GameUIPanel extends JFrame {
    public GameUIPanel() {
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(800, 600));
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());
        setBackground(Color.decode("#00a2e8"));

        // create and adjust Components
        UIManager.put("MenuBar.background", Color.decode("#00a2e8"));
        UIManager.put("MenuBar.selectionBackground", Color.decode("#0092e4"));

        UIManager.put("Menu.background", Color.decode("#00beff"));
        UIManager.put("Menu.selectionBackground", Color.decode("#00a2e8"));
        UIManager.put("Menu.foreground", Color.decode("#91593c"));
        UIManager.put("Menu.selectionForeground", Color.decode("#7e3c1a"));

        UIManager.put("MenuItem.background", Color.decode("#00beff"));
        UIManager.put("MenuItem.selectionBackground", Color.decode("#00a2e8"));
        UIManager.put("MenuItem.foreground", Color.decode("#91593c"));
        UIManager.put("MenuItem.selectionForeground", Color.decode("#7e3c1a"));

        UIManager.put("PopupMenu.border",
                BorderFactory.createLineBorder(Color.decode("#00beff"), 1));
        UIManager.put("PopupMenu.foreground", Color.decode("#000000"));

        JMenuBar menubar = new JMenuBar();
        menubar.setBorder(BorderFactory.createLineBorder(
                Color.decode("#7e3c1a"), 2, false));

        JMenu gameMenu = new JMenu("Game");
        gameMenu.setOpaque(true);
        gameMenu.setFont(Font.decode("Arial-BOLD-24"));

        JMenuItem back = new JMenuItem("Back");
        back.setFont(Font.decode("Arial-BOLD-24"));

        gameMenu.add(back);
        menubar.add(gameMenu);

        setJMenuBar(menubar);
        pack();
        setVisible(true);
    }
    public static void main(String[] args) {
        GameUIPanel gui = new GameUIPanel();
    }
}

Added an image, as requestet. I hope this helps. 3 different states of the JMenuBar; red marks on said borders

Almato
  • 53
  • 8
  • 2
    Note: the menu bar should be placed in a `JFrame` or `JDialog` using the appropriate `setJMenuBar(...)` method, not in its content pane and certainly not in a `JPanel`. See this related topic: [Why JMenuBar is not place in the JFrame content pane, but JToolbar place in the content pane](http://stackoverflow.com/questions/21660699/why-jmenubar-is-not-place-in-the-jframe-content-pane-but-jtoolbar-place-in-the) – dic19 Feb 27 '15 at 15:40
  • could you post a picture please? – ghostbust555 Feb 27 '15 at 16:04
  • @dic19 Thanks for the hint. Still, even though I might just want to change the whole thing to a `JToolBar`, the problem seems to persist when the `JMenuBar` is added to a `JFrame` instead, using `setJMenuBar(..)`. – Almato Feb 27 '15 at 16:10
  • @ghostbust555 done. =) Hope that helps. – Almato Feb 27 '15 at 16:25
  • 1
    I don't think it's a border, I think it's either a `JSeparator` or some other graphical element rendered by the look and feel – MadProgrammer Feb 27 '15 at 22:11
  • 1
    Might be best just to override the paint method for the component – ghostbust555 Feb 28 '15 at 18:19
  • @ghostbust555 Would this be the paint method of the JFrame or another one? I'm not sure whether or not the JMenuBar and other 'small' components have an own paint method. ^^ Could you possibly give a short example of what such an overridden method would look like? I currently can't imagine how to address the problem that way... Thanks :) – Almato Mar 01 '15 at 13:55
  • 1
    all swing components have there own paint method. So make your own JMenuBar extending class and override the paint there. Take a look at 3.0 of this https://www3.ntu.edu.sg/home/ehchua/programming/java/J4b_CustomGraphics.html (be aware of paint, paintComponent, and paintBorder. paint calls paintComponent and paintBorder so it may be better to try overriding paintBorder first) – ghostbust555 Mar 03 '15 at 16:17
  • 1
    also take a look at this http://nadeausoftware.com/articles/2008/08/java_tip_use_gradient_backgrounds_add_interest_and_polish_user_interface – ghostbust555 Mar 03 '15 at 16:20
  • 1
    @ghostbust555 Thanks. I guess it will take me some time to try this and post my results, as I am currently working on two other projects that are due this month. I'll post an answer in case I happen to come across another solution for this. – Almato Mar 04 '15 at 12:28

0 Answers0