Solved

How to add image to button

Posted on 2001-06-22
12
214 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
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!

 

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

Independent Software Vendors: 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

679 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