Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
private JPanel addToolbar() {    

		JPanel jp1=new JPanel();
		jp1.setLayout(new FlowLayout());  

		JButton jb7=new JButton("exit ");
		jb7.addActionListener(this);
		jb7.setForeground(new Color(00,00,255));	
		jp1.add(jb7);

here how to set the image near to the button field this is button create code but how can i set image on the button field
Posted
Updated 11-May-13 22:49pm
v5
Comments
Filipe Marques 4-Apr-13 4:32am    
Hi, look at this, I hope that helps you: http://stackoverflow.com/questions/3775373/java-how-to-add-image-to-jlabel

By reading your question I think that you want to add an image on your button. You could try something like this:
Java
import javax.imageio.ImageIO; //needed
import java.io.File;

    public void initialize(){

        //create your jframe
        //add you panel to it
        //add your button, set the actionListeners etc.
        File imageFile=new File("yourImage.png");
        Image image=ImageIO.read(imageFile);

        yourButton.setIcon(new ImageIcon(image));


    }


This should do the trick.
Best regards.
 
Share this answer
 
Hi,

Have an image tag and a button in your form and on the onclick event of the button, write something as below:

C#
protected void button_click()
{
ImageID.ImageUrl="Images/pic.jpg";
}


It should do the trick.
 
Share this answer
 
Java
JButton button = new JButton();
try {
  Image iconImg = ImageIO.read(getClass().getResource("resources/test.bmp"));
  button.setIcon(new ImageIcon(iconImg));
} catch (IOException ex) {
}

Here, image file is assumed to be inside src/resources folder.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900