Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change the coffee cup icon in JFrame

 
Murali Obla
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Niklas Junel
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 220
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Murli,
u need to change the line,
tm.setIconImage(frameIcon);
to
tm.setIconImage(frameIcon.getImage());
it will work.

Raj
 
Tris Rabar
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that's supposed to work Murli..

Good luck!
 
Murali Obla
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Raj and Trish,
 
Murali Obla
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Raj and Trish,

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

Murli
 
Tris Rabar
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic