How can i set an image as the background of JMenuBar in Windows LAF?

i overwrite JMenuBar's paintComponent method to draw background image, it works well except in Windows LAF, in which case, the background of menuitems is not the image drawed before, but the default white background. In other words, the background image only appears on the right of all the menuitems in the menubar, but don't appear as the backgound of menuitems.dont' know if i express me accurately with my poor english. Is there anyone can give me some advice to solve this problem? thanks!

[1245 byte] By [huza] at [2007-10-3 9:07:18]
# 1
hey buddy. Post some code that demonstrates your problem and we will check it out.
fireman.sparkeya at 2007-7-15 4:18:52 > top of Java-index,Desktop,Core GUI APIs...
# 2

> dont' know if i express me accurately with my poor english

Which is why we always ask for code. A picture is worth a thousand words.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-15 4:18:52 > top of Java-index,Desktop,Core GUI APIs...
# 3

hi, fireman.sparkey&& camickr , thanks for replies, my test codes are as follows:

// ImageMenuBar.java

import java.awt.Graphics;

import java.awt.Image;

import javax.swing.ImageIcon;

import javax.swing.JMenuBar;

public class ImageMenuBar extends JMenuBar {

private Image bkImage;

public ImageMenuBar() {

super();

getBkImage();

setOpaque(false);

}

public void paintComponent(Graphics g) {

g.drawImage(bkImage, 0, 0, getWidth(), getHeight(), this);

super.paintComponent(g);

}

private void getBkImage() {

bkImage = (new ImageIcon(getClass().getResource(

"menubar.jpg"))).getImage();

}

}

// MenuBarTest.java

import javax.swing.*;

public class MenuBarTest extends JFrame {

public MenuBarTest() {

super("Test MenuBar with image background");

init();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300, 200);

setVisible(true);

}

private void init() {

ImageMenuBar menuBar = new ImageMenuBar();

JMenu fileMenu = new JMenu("File");

JMenu helpMenu = new JMenu("Help");

JMenuItem openMT = new JMenuItem("Open");

JMenuItem exitMT = new JMenuItem("Exit");

JMenuItem aboutMT = new JMenuItem("About");

fileMenu.add(openMT);

fileMenu.add(exitMT);

helpMenu.add(aboutMT);

menuBar.add(fileMenu);

menuBar.add(helpMenu);

setJMenuBar(menuBar);

}

public static void main(String[] args) {

//try {

//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

//} catch (Exception e) {

//e.printStackTrace();

//}

new MenuBarTest();

}

}

put an image named "menubar.jpg" in the same directory with the two java file, and then run MenuBarTest. in default LAF(metal), image can fill the whol e menubar, but in Windows LAF, the area of menuitems can not display the image.

Huza at 2007-7-15 4:18:52 > top of Java-index,Desktop,Core GUI APIs...
# 4
In JDK1.4.2 on XP the image is displayed to the right of the Help menu in both the Metal and Windows LAF.If you want the image to appear under the File and Help menus then you need to set the menu non-opaque.
camickra at 2007-7-15 4:18:52 > top of Java-index,Desktop,Core GUI APIs...
# 5

Acturally I had tried this way before I posted this question, but it didn't work well.

I set Menu non-opaque, and the background of File and Help menus display the

image only when I move mouse onto them, when the mouse exit the menu, it

would disappear, The environment is OS:xp, JDK 1.4.2. If I replace JDK 1.4.2

with JDK1.5.0, the situation becomes worse. even though I move mouse onto

menus, their background are still default blue color, not the image :(

huza at 2007-7-15 4:18:52 > top of Java-index,Desktop,Core GUI APIs...
# 6

[nobr]hello

// ImageMenuBar.java

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.UIManager;

class ImageMenuBar extends JMenuBar

{

private ImagebkImage;

public ImageMenuBar()

{

super();

getBkImage();

setOpaque(false);

}

@Override

public void paintComponent(Graphics g)

{

g.drawImage(bkImage, 0, 0, getWidth(), getHeight(), this);

super.paintComponent(g);

}

private void getBkImage()

{

bkImage = (new ImageIcon(getClass().getResource("beach_05.jpg"))).getImage();

}

}

// MenuBarTest.java

public class MenuBarTest extends JFrame

{

public MenuBarTest()

{

super("Test MenuBar with image background");

init();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300, 200);

setVisible(true);

}

private void init()

{

ImageMenuBar menuBar = new ImageMenuBar();

JMenu fileMenu = new JMenu("File")

{

/**

* @author: aniruddha<br>

* @date: Nov 9, 2006, 11:10:25 AM<br>

* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)

*/

@Override

protected void paintComponent(Graphics g)

{

if(getParent() == null)

super.paintComponent(g);

else

{

//calculation is required... to find out exact x, y position

g.drawString(getText(), 6, 13);

}

}

/**

* @author: aniruddha<br>

* @date: Nov 9, 2006, 11:13:15 AM<br>

* @see javax.swing.AbstractButton#paintBorder(java.awt.Graphics)

*/

@Override

protected void paintBorder(Graphics g)

{

if(getParent() == null)

super.paintBorder(g);

}

};

fileMenu.setForeground(Color.red);

fileMenu.setOpaque(false);

JMenu helpMenu = new JMenu("Help");

helpMenu.setForeground(Color.red);

helpMenu.setOpaque(false);

JMenuItem openMT = new JMenuItem("Open");

JMenuItem exitMT = new JMenuItem("Exit");

JMenuItem aboutMT = new JMenuItem("About");

fileMenu.add(openMT);

fileMenu.add(exitMT);

helpMenu.add(aboutMT);

menuBar.add(fileMenu);

menuBar.add(helpMenu);

setJMenuBar(menuBar);

}

public static void main(String[] args)

{

//try

//{

//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

//}

//catch(Exception e)

//{

//e.printStackTrace();

//}

new MenuBarTest();

}

}

this is what you can try. like ImageMenuBar you may need to write new own Menu class. so that you can calculate the place of drawing the caption, and other basic things.

:)[/nobr]

Aniruddha-Herea at 2007-7-15 4:18:52 > top of Java-index,Desktop,Core GUI APIs...
# 7
it works fine, thanks you.
huza at 2007-7-15 4:18:52 > top of Java-index,Desktop,Core GUI APIs...