The code that follows is the start of a user interface that I am
creating for a Java Application. I am having troulb getting the
application to properly display the ImageIcon icon (which represents
canada.gif). Can anyone give me a clue as to why it is not displaying
properly. Below is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UserInterface extends JFrame {
JPanel contentPane;
ImageIcon icon = new ImageIcon("canada.gif");
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
JMenu menuHelp = new JMenu();
JMenuItem menuHelpAbout = new JMenuItem();
JLabel statusBar = new JLabel();
JLabel mapPic = new JLabel(icon);
JList airportList = new JList();
JPanel mapPanel = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel lowerPanel = new JPanel();
//Construct the frame
public UserInterface() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
statusBar.setText(" ");
statusBar.setBounds(new Rectangle(0, 0, 267, 15));
menuFile.setText("File");
menuFileExit.setText("Exit");
menuFileExit.addActionListener(new
UserInterface_menuFileExit_ActionAdapter(this));
menuHelp.setText("Help");
menuHelpAbout.setText("About");
menuHelpAbout.addActionListener(new
UserInterface_menuHelpAbout_ActionAdapter(this));
mapPic.setForeground(Color.white);
mapPic.setBorder(BorderFactory.createLineBorder(Color.black));
contentPane.setLayout(null);
mapPanel.setBounds(new Rectangle(68, 17, 267, 196));
mapPanel.setLayout(borderLayout1);
airportList.setBounds(new Rectangle(336, 18, 64, 197));
lowerPanel.setBounds(new Rectangle(-1, 214, 400, 86));
menuFile.add(menuFileExit);
menuHelp.add(menuHelpAbout);
menuBar.add(menuFile);
menuBar.add(menuHelp);
this.setJMenuBar(menuBar);
mapPanel.setLayout(new GridBagLayout());
mapPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(),"Map",javax.swing.border.TitledBorder.LEADING,javax.swing.border.TitledBorder.TOP,new
Font("SansSerif",0,11),new Color(60,60,60)));
mapPanel.add(mapPic);
contentPane.add(airportList, null);
contentPane.add(mapPanel, null);
contentPane.add(statusBar, null);
contentPane.add(lowerPanel, null);
}
//File | Exit action performed
public void menuFileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
//Help | About action performed
public void menuHelpAbout_actionPerformed(ActionEvent e) {
UserInterface_AboutBox dlg = new UserInterface_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
menuFileExit_actionPerformed(null);
}
}
}
Thanks,
Jeff
minsc - 20 May 2004 20:18 GMT
> ImageIcon icon = new ImageIcon("canada.gif");
Have you tried using an absolute path? (c:\...)
Regards,
Pieter Bonne.
Andrew Thompson - 20 May 2004 20:37 GMT
> The code that follows is the start of a user interface that I am
> creating for a Java Application. I am having troulb getting the
> application to properly display the ImageIcon icon (which represents
> canada.gif).
What does 'properly' mean?
Can you see it at all?
Are you getting exceptions?
Is it being displayed south of the US of A?
Wrong color?
> ..Can anyone give me a clue as to why it is not displaying
> properly.
If minsc understood you correctly, you
should be getting an exception, something
like 'IOException'. Please quote these
*word* for *word*, or better, copy/paste
them.
Check your exceptions or errors at the
Java Glossary on this page..
<http://mindprod.com/jgloss/errormessages.html>
HTH

Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology