How To Display Image On Jlabel Java Using Netbeans

How To Display Image On Jlabel Java Using Netbeans – Source Code

How To Display Image On Jlabel Java Using Netbeans

This code demonstartes how to display image on java jlabel using netbeans IDE. The image is loaded or chosen from your computer and then displayed on jlabel on the fly when you hit open button.

/*

 * How To Display Image In Jlabel Java Using Netbeans GUI App

 */

package displayimageonjavajlable;



import java.awt.Image;

import java.io.File;

import javax.swing.ImageIcon;

import javax.swing.JFileChooser;

import javax.swing.JOptionPane;

import javax.swing.filechooser.FileNameExtensionFilter;



/**

 *

 * @author Authentic

 */

public class DisplayImageOnJpanel extends javax.swing.JFrame {



    /**

     * Creates new form DisplayImageOnJpanel

     */

    public DisplayImageOnJpanel() {

        initComponents();

    }



    /**

     * This method is called from within the constructor to initialize the form.

     * WARNING: Do NOT modify this code. The content of this method is always

     * regenerated by the Form Editor.

     */

    @SuppressWarnings("unchecked")

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        

    private void initComponents() {



        jButton1 = new javax.swing.JButton();

        label = new javax.swing.JLabel();



        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);



        jButton1.setText("UPLOAD");

        jButton1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                jButton1ActionPerformed(evt);

            }

        });



        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);

        layout.setHorizontalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(layout.createSequentialGroup()

                .addGap(109, 109, 109)

                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addContainerGap(103, Short.MAX_VALUE))

            .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

        );

        layout.setVerticalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

                .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)

                .addGap(27, 27, 27))

        );



        pack();

        setLocationRelativeTo(null);

    }// </editor-fold>                      



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

        JFileChooser fileChooser = new JFileChooser();

        //Limit type of file name extensions supported.

        FileNameExtensionFilter filter = new FileNameExtensionFilter("4 Extensions Supported", "jpg", "png", "jpeg", "gif");

        fileChooser.setFileFilter(filter);

        int selected = fileChooser.showOpenDialog(null);

        //check if open button has been clicked.

        if (selected == JFileChooser.APPROVE_OPTION) {

            File file = fileChooser.getSelectedFile();

            //Get Path of the selected image.

            String getselectedImage = file.getAbsolutePath();

            //Display image path on Message Dialog

            JOptionPane.showMessageDialog(null, getselectedImage);

            ImageIcon imIco = new ImageIcon(getselectedImage);

            //make image fit on jlabel.

            Image imFit = imIco.getImage();

            Image imgFit = imFit.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);

            label.setIcon(new ImageIcon(imgFit));



        }

    }                                      



    /**

     * @param args the command line arguments

     */

    public static void main(String args[]) {

        /* Set the Nimbus look and feel */

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

         */

        try {

            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

                if ("Nimbus".equals(info.getName())) {

                    javax.swing.UIManager.setLookAndFeel(info.getClassName());

                    break;

                }

            }

        } catch (ClassNotFoundException ex) {

            java.util.logging.Logger.getLogger(DisplayImageOnJpanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

        } catch (InstantiationException ex) {

            java.util.logging.Logger.getLogger(DisplayImageOnJpanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

        } catch (IllegalAccessException ex) {

            java.util.logging.Logger.getLogger(DisplayImageOnJpanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

        } catch (javax.swing.UnsupportedLookAndFeelException ex) {

            java.util.logging.Logger.getLogger(DisplayImageOnJpanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

        }

        //</editor-fold>



        /* Create and display the form */

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new DisplayImageOnJpanel().setVisible(true);

            }

        });

    }



    // Variables declaration - do not modify                    

    private javax.swing.JButton jButton1;

    private javax.swing.JLabel label;

    // End of variables declaration                  

}


How To Display Image On Jlabel Java Using Netbeans

READ MORE  C Sharp DataGridView Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *