Jiun Jye Jiun Jye - 1 year ago 109
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();
File f=chooser.getSelectedFile();


Answer Source

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.
 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
 catch(IOException ioe){
 System.out.println("Exception occured while setting Image on the Label!");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download