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
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
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
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();
}
}
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();
}
}
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.