Solved

How to add image to button

Posted on 2001-06-22
12
210 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

786 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