wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to change the coffee cup icon in JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to change the coffee cup icon in JFrame" Watch "How to change the coffee cup icon in JFrame" New topic
Author

How to change the coffee cup icon in JFrame

Murali Obla
Ranch Hand

Joined: Nov 18, 2001
Posts: 40
Hello All,

I have a simple doubt that How can i change i mean how to set the icon in my JFrame.By defaulf this will set to coffee cup icon.I have to change it to some other icon.How can i do that?Pls help me.
Thanks in Advance,
Murli


"Excellence is never an accident; it is always the result of high intention, sincere effort, intelligent direction, skillful execution, and the vision to see obstacles as opportunities"
Niklas Junel
Greenhorn

Joined: Dec 19, 2001
Posts: 11
The first thing you have to do is to make a icon with width=16px and height=16px.
Import the image in a ImageIcon-object in your application.
Then you just call the method setIconImage() on your Frame.
//example
MyFrame.setIconImage(MyImageIcon);
Murali Obla
Ranch Hand

Joined: Nov 18, 2001
Posts: 40
Originally posted by Murali Sivanath:
Hello All,

I have a simple doubt that How can i change i mean how to set the icon in my JFrame.By defaulf this will set to coffee cup icon.I have to change it to some other icon.How can i do that?Pls help me.
Thanks in Advance,
Murli
Murali Obla
Ranch Hand

Joined: Nov 18, 2001
Posts: 40
Thanks for ur reply Niklas.
Any how I have tried that.It's not working
I'll send my code
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestMenu extends JFrame
{
private Container contentPane;
private JTextArea workspace;
private JToolBar jtb;
String[] aMenus = {"File","Edit","Look and Feel","About","Help"};
String[][] aItems ={{"New","Open",GMenu.SEPARATOR,"Save","Save As..",GMenu.SEPARATOR,"Exit"},{"Copy","Paste"},{"Windows","Java"},{"About.."},{"Help Topics","Reference"}};
//gm[4].setSubMenuItems({"s1","s2"});
//String[] aFileItems ={"New","Open"};
//String[] aAboutItems ={"About.."};
//Image frameIcon=new Image();
//String frameIconfile="open.gif";
String iconfile[]={"images/open.gif","images/save.gif"};
String tiptext[]={"Open an existing file","Save the current changes"};
ImageIcon icon[]=new ImageIcon[iconfile.length];
JButton btn[]=new JButton[iconfile.length];
private ButtonGroup bg;
TestMenu()
{
setLookAndFeelFrame();
GMenuBar gmb = new GMenuBar();
gmb.setMenu(aMenus);
Vector gm = gmb.getMenu();
//gm[] = gmb.getMenu();
/*((GMenu)gm.get(0)).setMenuItems(aFileItems);
((GMenu)gm.get(1)).setMenuItems(aAboutItems);*/
//gm[0].setMenuItems(aFileItems);
//gm[1].setMenuItems(aAboutItems);
for(int z= 0;z< gm.size();z++)
{
((GMenu)gm.get(z)).setMenuItems(aItems[z]);
}
/*for(int i=0;i<gm.size();i++)
{
for(int j=0;j<GMenu.menuItems.size();j++)
{
}
}*/
setJMenuBar(gmb);
//Tool Bar
jtb=new JToolBar();
bg=new ButtonGroup();
for(int i=0;i<iconfile.length;i++)
{
icon[i]=new ImageIcon(iconfile[i]);
btn[i]=new JButton(icon[i]);
btn[i].setToolTipText(tiptext[i]);
//btn[i].addActionListener(this);
jtb.add(btn[i]);
if(i==2)
jtb.addSeparator();
if(i>2)
bg.add(btn[i]);
}
Container contentPane = getContentPane();
workspace = new JTextArea(5,30);
workspace.setEditable(false);
contentPane.add(jtb,BorderLayout.NORTH);
contentPane.add(workspace,BorderLayout.CENTER);
setLocation(150,150);
setSize(350,300);
show();
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void setLookAndFeelFrame()
{
try
{
//UIManager.setLookAndFeel(
//"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e){System.out.println("Ex "+e);}
}
public static void main(String arg[])
{
//frameIcon=(Image)frameIcon;
ImageIcon frameIcon=new ImageIcon("open.gif");
TestMenu tm = new TestMenu();
tm.setIconImage(frameIcon);
//tm.setIconImage();
}
private void jbInit() throws Exception {
this.setDefaultCloseOperation(3);
}
}
It's showing the following error
Error: (101) method setIconImage(javax.swing.ImageIcon) not found in class TestMenu.

expecting reply,
Murli
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Hi Murli,
u need to change the line,
tm.setIconImage(frameIcon);
to
tm.setIconImage(frameIcon.getImage());
it will work.

Raj
Tris Rabar
Ranch Hand

Joined: Feb 27, 2002
Posts: 72
Yep, that's supposed to work Murli..

Good luck!


- Trish -
Murali Obla
Ranch Hand

Joined: Nov 18, 2001
Posts: 40
Hai Raj and Trish,
Murali Obla
Ranch Hand

Joined: Nov 18, 2001
Posts: 40
Hai Raj and Trish,

Thanks for ur help.It works.Thank u very much

Murli
Tris Rabar
Ranch Hand

Joined: Feb 27, 2002
Posts: 72
You're welcome!!
 
wood burning stoves
 
subject: How to change the coffee cup icon in JFrame