Click here to Skip to main content
12,064,546 members (25,465 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java Java SE J2EE
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 3-Apr-13 21:46pm
Edited 11-May-13 23: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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

By reading your question I think that you want to add an image on your button. You could try something like this:
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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

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

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

It should do the trick.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160204.4 | Last Updated 14 May 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100