Cant change window Icon in a JFrame

I cannot seem to set the icon in the upper left of the window or in the task. I am using NetBeans and it created my JFrame for me using the following code to initialize:

java.awt.EventQueue.invokeLater(new Unable(){

publicvoid run(){

new MainFrame().setVisible(true);

}

});

I have checked online tutorials and this seem to be a trully easy task BUT my JFrame, MainFrame, seems not to have the method I need....

I should be able too do this:

MainFrame.setIconImage(Image image);

But it seems I cannot get that method in my JFrame at all, it just doesnt show up in NetBeans and wont allow me to compile. I have tried looking at the base JFrame class and see no setIconImage there either. Any help would be apriciated.

KN

[1058 byte] By [Biffsnota] at [2007-10-3 2:21:10]
# 1
Tell me what's wrong with this:System.out.println(String "Hello World");
floundera at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 2

lol I copyied and pasted that fron the net off a how-too site. Its just an ecample, not my code. He obviously was showing an imag goes there. My problem stems from a deeper issue than than. I cannot, repeat cannot even get the setIconImage method to show. Or compile. Or anything. Its not whats in the () thats screwing me up, its the missing method. JFrame should have it, it doesnt as far as netbeans is telling me. java.awt.Frame should have it, it doesnt as far as netbeans tells me. I cant follow the tutorials online without access to the method, you see what I am saying?

Biffsnota at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 3
Do you mind posting the exact error message you get? And possibly the line of code it refers to.Mike
bellyrippera at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 4

Ok, here is the compile time message from NetBeans:

E:\Java Apps\Calendar\src\Main\MainFrame.java:690: cannot find symbol

symbol : method setIconImage()

location: class Main.MainFrame

MainFrame.setIconImage();

1 error

BUILD FAILED (total time: 1 second)

I dont know if you use netbeans but it has an autofill feature that shows pop-up boxes of the packages or whatever as you type. It will, for example, show me the toString() method if I type Integer. and allow me to complete it to Integer.toString().

It does not show and set methods for my frame at all. It shows lots of other things, but no set methods.

I should be able to do this:

frame.setIconImage(

Toolkit.getDefaultToolkit().getImage("images/jr.jpg"));

><

KN

Biffsnota at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 5
Can you post the code for your MainFrame class. Use code tags.
floundera at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 6

> Can you post the code for your MainFrame class. Use

> code tags.

Ok, 1st excuse the messy code, I am teaching myself Java and the code is a bit unproffesional. Also the code is large as it is a calendar program that allows you to input text into the actual calendar day boxes, loads, saves etc. Here is the declaration of the MainFrame class and the MainFrame constructor.

public class MainFrame extends javax.swing.JFrame {

// Constants

public final int DATE_HEIGHT = 17;

public final int DAY_HEIGHT = 80;

public final boolean DBG = true;

public boolean Init = true;

public int curMonth = 0;

public int curYear = 0;

/** Creates new form MainFrame */

public MainFrame() {

initComponents();

for(int x = 0;x < 12;x = x + 2){

jTable1.setRowHeight(x,DATE_HEIGHT);

jTable1.setRowHeight(x+1,DAY_HEIGHT);

}

jTable1.setDefaultRenderer(Object.class, new ColorCellRenderer());

DateFormat dateFormat =

DateFormat.getDateInstance(DateFormat.FULL);

// Create our Gregorian Calendar.

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(new Date());

DaYear.setYear(cal.get(cal.YEAR));

DaMonth.setMonth(cal.get(cal.MONTH));

Init = false;

LoadIt(curMonth, curYear);

}

KN

Biffsnota at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 7
Only thing I can think of is that you have two copies of MainFrame class in different directories. One that doesn't extend JFrame and it is this one you are trying to create an object of.
floundera at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 8

> Only thing I can think of is that you have two copies

> of MainFrame class in different directories. One that

> doesn't extend JFrame and it is this one you are

> trying to create an object of.

I have just checked the dir structure as well as the code and that is the only file, period. And if I type in javax.swing.JFrame.it shows me 2 methods (isDefaultLookAndFeel and setDefaultLookAndFeel) and a slew of constants, nothing more. I dont understand this, even using the base class wont let me acces something referenced in 20 tutorial sites ><

Any other ideas?

KN

Biffsnota at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 9
Try compiling from the command line. Sounds like Netbeans is a little screwy.
floundera at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 10
It looks like you are trying to invoke the method on the MainFrame class (i.e. as a static method) and not on an instance of that class. The setIconImage(...) is an instance method, not a class (static) one.Mike
bellyrippera at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 11

> It looks like you are trying to invoke the method on

> the MainFrame class (i.e. as a static method) and not

> on an instance of that class. The setIconImage(...)

> is an instance method, not a class (static) one.

>

> Mike

Ok. This looks like it may be it. I am not sure why though because I have obviously made an instance in order to show my GUI. So then how do I specify the instance rather than the class?

I just found out that

this.

has a setIconImage method, so is this.setImageIcon()

refering to the instance? Sorry if this seems dumb.

KN

Biffsnota at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 12

> I just found out that

>

> this.

>

> has a setIconImage method, so is

> this.setImageIcon()

refering to the

> instance? Sorry if this seems dumb.

Yes, that will refer to the instance. But, even simpler, you can use it without specifying "this" (the "this" is implied):

setImageIcon(someIcon);

MLRona at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 13

Changing the Icon is easy, it's reading a .JPG/whatever into an image that is a little tricky.

copy this method in and pass the name of the file you want to read into it.

/*This method takes a filename as a string and gives an image

from that filename*/

public static Image getImageFromFile(String s)

{

Image i = null;

File f = new File(s);

try {

i = ImageIO.read(f);

}

catch(IOException e)

{System.out.println("Bad things happened");}

return i;

}//end getImageFromFile(String s)

-

Now use this sort of call

frame.setIconImage(getImageFromFile("IMAGENAMEHERE.JPG");

kuriyama@woh.rr.coma at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 14

I too am having this problem.

I'm not a beginner, however the few times I've had to change the icon I've found an example that works, and forget how to do it.

Here is what I'm doing now and it is not working:

Image icon = new ImageIcon("memory.GIF").getImage();

setIconImage(icon) ;

Also, tried scaling the image

Image icon = new ImageIcon("memory.GIF").getImage();

setIconImage(icon.getScaledInstance(10, 10, image.SCALE_DEFAULT));

The gif file exists in the same directory as the class.

Doesn't Work.

penguinman123a at 2007-7-14 19:20:06 > top of Java-index,Java Essentials,New To Java...
# 15
The Example from Kuriyama worked.As long as the full path of the image is passed to the load method.Thanks.
penguinman123a at 2007-7-21 9:52:58 > top of Java-index,Java Essentials,New To Java...