Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

How to add image to button

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
uron6342
Asked:
uron6342
  • 6
  • 5
1 Solution
 
ozymandiasCommented:
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
 
ozymandiasCommented:
Look at the Java API docs for JButton.

You can set an Icon, PressedIcon, SelectedIcon, RolloverIcon, DisabledIcon,
0
 
ozymandiasCommented:
Oooops...hit enter by mistake...

Basically, lots of different icons...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
uron6342Author Commented:
0
 
uron6342Author Commented:
0
 
uron6342Author Commented:
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
 
uron6342Author Commented:
hmm. sometime get error messages here when i post code.
0
 
ozymandiasCommented:
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
 
uron6342Author Commented:
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
 
ozymandiasCommented:
No problem. Glad I could help.
0
 
RajeshTandelCommented:
hi friend this is how u add icon to ur button

JButton button = new JButton("Pointer",new ImageIcon("IconFile/Pointer.gif"));
0
 
ozymandiasCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now