Jiun Jye Jiun Jye - 23 days ago 6
Java Question

netbeans: Attach image in Jlabel using Jbutton

How to create a personal detail which I need to attach to a personal image? When clicking the button, it can select the picture, but I still cannot figure it out how to set this coding, any one can help solve this?

JFileChooser chooser=new JFileChooser();
chooser.showOpenDialog(null);
File f=chooser.getSelectedFile();

personal_image.

Answer

As I noted that you're using NetBeans IDE, NetBeans already provides the facility to write the actionPerformed for a JButton in case of an ActionEvent.

I suggest you write the following code in your JButton's actionPerformed with ActionEvent evt as mentioned below :-

private void DesiredButtonActionPerformed(java.awt.event.ActionEvent evt){

 // Add your mentioned code here before coding this.
 try{
 Image image = ImageIO.read(f);
 ImageIcon icon = new ImageIcon(image);
 // JLabel toBeSet = new JLabel(); 
 // considering that you have a JLabel having name as what I've used here
 toBeSet.setIcon(icon);
 }
 catch(IOException ioe){
 System.out.println("Exception occured while setting Image on the Label!");
 }

}
Comments