Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java J2EE
private JPanel addToolbar() {    
 
<pre>
    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);</pre>

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 at 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;
 
<pre>
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
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 310
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210


Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 14 May 2013
Copyright © CodeProject, 1999-2015
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