Solved

How to add image to button

Posted on 2001-06-22
12
209 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
  • 6
  • 5
12 Comments
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
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
Comment Utility
Look at the Java API docs for JButton.

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

Expert Comment

by:ozymandias
Comment Utility
Oooops...hit enter by mistake...

Basically, lots of different icons...
0
 

Author Comment

by:uron6342
Comment Utility
0
 

Author Comment

by:uron6342
Comment Utility
0
 

Author Comment

by:uron6342
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

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

Accepted Solution

by:
ozymandias earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
No problem. Glad I could help.
0
 

Expert Comment

by:RajeshTandel
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now