Hello,

I am trying to work through an example from a Deitel book. I keep getting a "source not found" error on line 27 of the LabelFrame class. I have put the "bug1.png" in every single directory associated with this project. I am stumped...

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class LabelFrame extends JFrame
{
	private JLabel label1;
	private JLabel label2;
	private JLabel label3;
		
	// LabelFrame constructor adds JLabels to JFrame (This is the constructor)
	public LabelFrame()
	{
		super( "Testing JLabel" );
		setLayout( new FlowLayout()); // set frame layout
		
		// JLabel constructor with a string argument
		label1 = new JLabel( "Label with text" );
		label1.setToolTipText( "This is label1" );
		add (label1);
		
		// JLabel constructor with string, Icon and alignment arguments
		Icon bug = new ImageIcon( getClass().getResource ("bug1.png"));
		label2 = new JLabel( "Label with text and icon", bug, 
				SwingConstants.LEFT);
		label2.setToolTipText("This is label2");
		add (label2);
		
		label3 = new JLabel();
		label3.setText("Label with icon and text at bottom)");
//		label3.setIcon(bug);
		label3.setHorizontalTextPosition(SwingConstants.CENTER);
		label3.setVerticalTextPosition(SwingConstants.BOTTOM);
		label3.setToolTipText("This is label3");
		add (label3); // add label3 to JFrame
	} // end LabelFrame constructor
} // end Class LabelFrame
import javax.swing.JFrame;

//
// Testing LabelFrame

public class LabelTest 
{
	public static void main(String[] args) 
	{
		LabelFrame labelFrame = new LabelFrame();
		labelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
		labelFrame.setSize( 260, 180);
		labelFrame.setVisible( true);
	} // end main
} // end Class LabelTest

Edited 4 Years Ago by coolbeanbob: n/a

Just a wild stab in the dark, but try this.getClass()

That did not work either.

I think I accidentally flagged your post as bad. Sorry for that.

You could try getting rid of getClass().getResource and just put in the image name. Whenever I make an ImageIcon, I usually only put the file path in parentheses. Try it, see how it works.

You could try getting rid of getClass().getResource and just put in the image name. Whenever I make an ImageIcon, I usually only put the file path in parentheses. Try it, see how it works.

This time it compiled, but with no image.

I tried the program and got it to work...
Did you put the image in the same directory with the class files?
are you sure that the image has the right filename and extension?

I tried the program and got it to work...
Did you put the image in the same directory with the class files?
are you sure that the image has the right filename and extension?

I'm pretty sure I have it in the same class folder. I have "bug1.png" in every single folder under the LabelFrame directory.

When I downloaded the image, I renamed it and added the ".png", could this be the problem?

Yeah that might be it...if you don't have file extensions for known file types visible and then forcefully add .png to the end then it could ruin your intentions

that could be it, rename it as bug1 and check the extension if its a png file, if not convert it

How do I check the extension? When I go to Properties, the "Type of file" field says "Paint Shop Pro X Image"

It should be PNG Image (.png)
Try converting it to png image, there are lots of converters on the web

Edited 4 Years Ago by zeroliken: n/a

Save the image as a PNG, JPEG, or GIF from Paint Shop Pro. ImageIcon accepts those three file types.

Well there's your problem, open it with paint or paint.net or whatever you have and save it (with another name just in case), png is portable network graphics, paintshop file extension is .psp. Just adding .png to an image won't convert it to png

This article has been dead for over six months. Start a new discussion instead.