Solved

How to add image to button

Posted on 2001-06-22
12
216 Views
Last Modified: 2010-03-31
hello-

how do i place a gif or icon on a jbutton? or i guess it doesnt have to be a button. just something that will create an event when clicked on.

thanks- u
0
Comment
Question by:uron6342
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 6221229
ImageIcon i = new ImageIcon("myImageFile.gif");
JButton jb = new JButton();
jb.setIcon(i);


You can also sepcify the icon in the contructor :

JButton jb = new JButton(i);


0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6221231
Look at the Java API docs for JButton.

You can set an Icon, PressedIcon, SelectedIcon, RolloverIcon, DisabledIcon,
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6221232
Oooops...hit enter by mistake...

Basically, lots of different icons...
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:uron6342
ID: 6222322
0
 

Author Comment

by:uron6342
ID: 6222325
0
 

Author Comment

by:uron6342
ID: 6222326
i am having problems compiling this. here is my code
and error message. thanks!


import java.awt.*;
import javax.swing.*;
import com.symantec.itools.javax.swing.icons.ImageIcon;

/**
* A basic extension of the javax.swing.JApplet class
*/
public class JApplet1 extends JApplet
{
public void init()
{
// Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
symantec.itools.lang.Context.setApplet(this);

// This line prevents the "Swing: checked access to system event queue" message seen in some browsers.
getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);

// This code is automatically generated by Visual Cafe when you add
// components to the visual environment. It instantiates and initializes
// the components. To modify the code, only use code syntax that matches
// what Visual Cafe can generate, or Visual Cafe may be unable to back
// parse your Java file into its visual environment.
//{{INIT_CONTROLS
getContentPane().setLayout(null);
setSize(987,511);
btnLighting.setActionCommand("Lighting");
getContentPane().add(btnLighting);
btnLighting.setBounds(444,48,48,48);
JButton2.setText("jbutton");
JButton2.setActionCommand("jbutton");
getContentPane().add(JButton2);
JButton2.setBounds(444,108,48,48);
JButton3.setText("jbutton");
JButton3.setActionCommand("jbutton");
getContentPane().add(JButton3);
JButton3.setBounds(444,168,48,48);
JButton4.setText("jbutton");
JButton4.setActionCommand("jbutton");
getContentPane().add(JButton4);
JButton4.setBounds(444,228,48,48);
JButton5.setText("jbutton");
JButton5.setActionCommand("jbutton");
getContentPane().add(JButton5);
JButton5.setBounds(444,288,48,48);
JLabel1.setText("This is some incoming text");
getContentPane().add(JLabel1);
JLabel1.setBounds(12,480,904,16);
getContentPane().add(JTextField1);
JTextField1.setBounds(12,456,892,24);
getContentPane().add(JTextField2);
JTextField2.setBounds(528,48,396,372);
getContentPane().add(JTextField3);
JTextField3.setBounds(12,48,420,372);
JLabel2.setText("San Francisco");
getContentPane().add(JLabel2);
JLabel2.setBounds(144,24,132,24);
JLabel3.setText("New York");
getContentPane().add(JLabel3);
JLabel3.setBounds(636,24,132,24);
JButton6.setText("jbutton");
JButton6.setActionCommand("jbutton");
getContentPane().add(JButton6);
JButton6.setBounds(444,348,48,48);

//}}

i = new ImageIcon("Ligthing.gif");
btnLighting.setIcon(i);


}


ImageIcon i;



//{{DECLARE_CONTROLS
javax.swing.JButton btnLighting = new javax.swing.JButton();
javax.swing.JButton JButton2 = new javax.swing.JButton();
javax.swing.JButton JButton3 = new javax.swing.JButton();
javax.swing.JButton JButton4 = new javax.swing.JButton();
javax.swing.JButton JButton5 = new javax.swing.JButton();
javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
javax.swing.JTextField JTextField1 = new javax.swing.JTextField();
javax.swing.JTextField JTextField2 = new javax.swing.JTextField();
javax.swing.JTextField JTextField3 = new javax.swing.JTextField();
javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
javax.swing.JLabel JLabel3 = new javax.swing.JLabel();
javax.swing.JButton JButton6 = new javax.swing.JButton();
//}}
}

Error: D:\dev\weather\JApplet1.java(67): Incompatible type for constructor. Can't convert java.lang.String to java.io.InputStream.
i = new ImageIcon("Ligthing.gif");
^
1 error
Build Failed
0
 

Author Comment

by:uron6342
ID: 6222328
hmm. sometime get error messages here when i post code.
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 50 total points
ID: 6222532
OK. I have compiled and run it fine, by removing the line :

  import com.symantec.itools.javax.swing.icons.ImageIcon;

Why have you imported this class ?
You already have javax.swing.ImageIcon.
I cannot tell you what is wrong because I do not have the docs for com.symantec.itools.javax.swing.icons.ImageIcon, but I suspect that this class does not take a string as an augument in any of its constructors, or it wants an InputStream or something.

Also, your button is called lighting but your gif name is spelt "ligthing". Is this a typo ?
0
 

Author Comment

by:uron6342
ID: 6223131
great thanx. yes, that was a typo. i do that stuff when im up too late. i also removed the symantec import. i am using visual cafe and it includes all those symantecthings i dont want. ill have to be more careful about that in the future. thanks again.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6223495
No problem. Glad I could help.
0
 

Expert Comment

by:RajeshTandel
ID: 6240460
hi friend this is how u add icon to ur button

JButton button = new JButton("Pointer",new ImageIcon("IconFile/Pointer.gif"));
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6240653
Rajesh, I'm guessing that you're new to EE.
a) uron6342 has already accepted and answer to this question, and
b) the answer you gave was already covered in my comments above.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question