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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
Yep, that's supposed to work Murli..
Good luck!
- Trish -
Murali Obla
Ranch Hand
Joined: Nov 18, 2001
Posts: 40
posted
0
Hai Raj and Trish,
Murali Obla
Ranch Hand
Joined: Nov 18, 2001
Posts: 40
posted
0
Hai Raj and Trish,
Thanks for ur help.It works.Thank u very much Murli