why is this code not displaying the image?

Hi,

ImageIcon myImage =new ImageIcon("image.jpg");

JButton jButton1 =new JButton(myImage);

Can someone tell me why that code doesn't show the image? The image is in the same location where the source files are.

[329 byte] By [iPortala] at [2007-11-27 2:27:55]
# 1
Can you give a larger view of your code?
Satanduvela at 2007-7-12 2:39:14 > top of Java-index,Java Essentials,New To Java...
# 2

> Can you give a larger view of your code?

package arabiclanguagelearning;

import java.awt.BorderLayout;

import java.awt.Dimension;

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.GridLayout;

import java.awt.Rectangle;

import javax.swing.JButton;

import java.awt.FlowLayout;

import java.awt.Color;

import javax.swing.UIManager;

import java.awt.SystemColor;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

/**

*

Title: Arabic Language Learning

*

*

Description:

*

*

Copyright: Copyright (c) 2007

*

*

Company:

*

* @author iPortal

* @version 1.0

*/

public class FrontPage extends JFrame {

JPanel contentPane;

JPanel jPanel1 = new JPanel();

JButton reading = new JButton();

JButton writing = new JButton();

GridLayout gridLayout1 = new GridLayout();

JButton listening = new JButton();

JButton speaking = new JButton();

Dialog1 myDialog = new Dialog1(this,"title",true);

ImageIcon myImage = new ImageIcon("image.jpg");

JButton jButton1 = new JButton(myImage);

public FrontPage() {

try {

setDefaultCloseOperation(EXIT_ON_CLOSE);

jbInit();

} catch (Exception exception) {

exception.printStackTrace();

}

}

/**

* Component initialization.

*

* @throws java.lang.Exception

*/

private void jbInit() throws Exception {

contentPane = (JPanel) getContentPane();

contentPane.setLayout(null);

setSize(new Dimension(524, 423));

setTitle("Choose a tutorial");

jPanel1.setBackground(SystemColor.infoText);

jPanel1.setFont(new java.awt.Font("Comic Sans MS", Font.PLAIN, 11));

jPanel1.setBounds(new Rectangle(80, 158, 365, 67));

jPanel1.setLayout(gridLayout1);

reading.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 13));

reading.setToolTipText("Take the Reading Tutorial");

reading.setText("Reading");

reading.addActionListener(new FrontPage_reading_actionAdapter(this));

writing.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 13));

writing.setText("Writing");

listening.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 13));

listening.setText("Listening");

speaking.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 13));

speaking.setText("Speaking");

jButton1.setBounds(new Rectangle(78, 273, 169, 57));

//jButton1.setText("new button");

contentPane.add(jPanel1);

contentPane.add(jButton1);

jPanel1.add(reading, null);

jPanel1.add(writing, null);

jPanel1.add(listening);

jPanel1.add(speaking);

}

public void reading_actionPerformed(ActionEvent e) {

myDialog.setSize(600,600);

myDialog.setVisible(true);

}

}

class FrontPage_reading_actionAdapter implements ActionListener {

private FrontPage adaptee;

FrontPage_reading_actionAdapter(FrontPage adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.reading_actionPerformed(e);

}

}

iPortala at 2007-7-12 2:39:14 > top of Java-index,Java Essentials,New To Java...
# 3

and if you try

JButton jButton1 = new JButton(new ImageIcon ("image.jpg"));

and if that doesn't work whats happen when you put this line in commantary

jButton1.setBounds(new Rectangle(78, 273, 169, 57));

Satanduvel

Satanduvela at 2007-7-12 2:39:14 > top of Java-index,Java Essentials,New To Java...
# 4
thanks for your help, but still get the same problem after trying both your suggestions.
iPortala at 2007-7-12 2:39:14 > top of Java-index,Java Essentials,New To Java...
# 5
and when you put some text in the Jbutton? Not a icon. Is the button with text then displayed?
Satanduvela at 2007-7-12 2:39:14 > top of Java-index,Java Essentials,New To Java...
# 6
Yes, text is displayed fine. If you notice i commented out the line below//jButton1.setText("jButton1");As i thought it might be overriding the icon image.
iPortala at 2007-7-12 2:39:14 > top of Java-index,Java Essentials,New To Java...
# 7
maybe you can try to put these lines in the init part.ImageIcon myImage = new ImageIcon("image.jpg");JButton jButton1 = new JButton(myImage);Satanduvel
Satanduvela at 2007-7-12 2:39:14 > top of Java-index,Java Essentials,New To Java...