JMenuBar - Icon for JMenuItem is not displayed

Hi all

This is the (condensed) code I am using:

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

public class MainFrame extends JFrame implements ActionListener {

public MainFrame()

setTitle("Application Title");

setSize(800, 600);

//MenuBar

JMenuBar mBar = new JMenuBar();

//File Menu

JMenu fileMenu = new JMenu("File");

fileMenu.setMnemonic('f');

JMenuItem file;

Icon exitIcon = new ImageIcon("Exit16.gif");

file = new JMenuItem("Exit",exitIcon);

file.setMnemonic('x');

file.addActionListener(this);

file.setAccelerator(KeyStroke.getKeyStroke(

KeyEvent.VK_X, InputEvent.CTRL_MASK));

fileMenu.add(file);

mBar.add(fileMenu);

setJMenuBar(mBar);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

public static void main(String[] args) {

MainFrame mf = new MainFrame();

}

}

When the JFrame is run, the Icon is not displayed. Everything else works fine.

Any help much apprectiated

Karlo

[1204 byte] By [karlomutschler] at [2007-9-27 22:31:41]
# 1

Are you sure that icon is succesfully created?

Icon exitIcon = new ImageIcon("Exit16.gif");

System.out.println(exitIcon.getIconWidth());

System.out.println(exitIcon.getIconHeight());

file = new JMenuItem("Exit",exitIcon);

if this icon is invalid these values will be -1

best regards

Stas

StanislavL at 2007-7-7 13:10:06 > top of Java-index,Archived Forums,Swing...
# 2

ImageIcon doesn't throws a Exception if the image is not found, so try:

1.set the path absolute like c:/temp/Exit16.gif

2.if it works the cause is maybe the search path (you use an IDE for executing?) so try:

ImageIcon exitIcon = new ImageIcon(this.getClass().getResource("Exit16.gif"));

3.why you use Icon instead of ImageIcon?

hope that helps

mahtan at 2007-7-7 13:10:06 > top of Java-index,Archived Forums,Swing...
# 3

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

public class MainFrame extends JFrame implements ActionListener {

public MainFrame(){

setTitle("Application Title");

setSize(800, 600);

//MenuBar

JMenuBar mBar = new JMenuBar();

//File Menu

JMenu fileMenu = new JMenu("File");

fileMenu.setMnemonic('f');

JMenuItem file;

Icon exitIcon = new ImageIcon("Exit16.gif");

file = new JMenuItem("Exit",exitIcon);

file.setMnemonic('x');

file.addActionListener(this);

file.setAccelerator(KeyStroke.getKeyStroke(

KeyEvent.VK_X, InputEvent.CTRL_MASK));

fileMenu.add(file);

mBar.add(fileMenu);

setJMenuBar(mBar);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public void actionPerformed(ActionEvent e){

}

public static void main(String[] args) {

MainFrame mf = new MainFrame();

}

}

migoran at 2007-7-7 13:10:06 > top of Java-index,Archived Forums,Swing...
# 4

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

public class MainFrame extends JFrame implements ActionListener {

public MainFrame(){

setTitle("Application Title");

setSize(800, 600);

//MenuBar

JMenuBar mBar = new JMenuBar();

//File Menu

JMenu fileMenu = new JMenu("File");

fileMenu.setMnemonic('f');

JMenuItem file;

Icon exitIcon = new ImageIcon("Exit16.gif");

file = new JMenuItem("Exit",exitIcon);

file.setMnemonic('x');

file.addActionListener(this);

file.setAccelerator(KeyStroke.getKeyStroke(

KeyEvent.VK_X, InputEvent.CTRL_MASK));

fileMenu.add(file);

mBar.add(fileMenu);

setJMenuBar(mBar);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public void actionPerformed(ActionEvent e){

}

public static void main(String[] args) {

MainFrame mf = new MainFrame();

}

}

migoran at 2007-7-7 13:10:06 > top of Java-index,Archived Forums,Swing...
# 5

Hi all and thank you for your valued comments:

I have extended the JMenuBar: it now consists of FileMenu and an EditMenu.

The FileMenu contains the MenuItems Printer Setup and Exit (with ImageIcon "Exits.gif")

The EditMenu contains the MenuItems Cut, Copy and Paste; each one with the respective ImageIcon.

The ImageIcons for the MenuItems Cut and Copy are not displayed, while the ImageIcons for Exit and Paste are. I have used the "absolute path" method (c:/temp/Exits.gif) as well as the code (this.getClass().getResource("Exits.gif")).

The ImageIcons are all in the same directory as MainFrame.java and MainFrame.class.

I compile and run using Forte; but even compiling and running from the command line delivers the same result.

Any ideas why ?

Thanks and kind regards.

karlomutschler at 2007-7-7 13:10:06 > top of Java-index,Archived Forums,Swing...