Yuita A. Sari

“If you want to be a writer, you must do two things : read a lot and write a lot. There's no way around these two things that I'm aware of, no shortcut.” ― Stephen King

Load Image dalam JInternalFrame

1 Comment

Post ini dibuat dengan salah satu alasan yaitu mengurangi penasaran saya terhadap pertanyaan teman. Pertanyaannya adalah bagaimana load image di internal frame sehingga pada nantinya framenya bisa diminimize/maximize. Oh ya hampir lupa, kita disini belajar mengenai Java, jadi program yang akan kita pakai juga java. Saya menggunakan Netbeans 7.0, dimana untuk look and feel user interfacenya, saya masih import manual.😀 Kurang lebih output programnya seperti ini.

Program dijalankan kemudian klik tombol browse, yang artinya memilih file gambar yang akan diinginkan, disini saya tidak menggunakan file filter untuk menyaring mana aja file yang akan diambil sebagai gambar. Setelah gambar dipilih dan klik tombol Open maka akan keluar tampilan seperti dibawah :

kGambar akan otomatis muncul dalam internalframe, dan selanjutnya bisa di minimize dan di maximize.🙂

Baiklah kita langsung buka saja netbeansnya, silakan buat project baru, dan buat JFrame baru, silakan design sesuka hati. Tinggal drag and drop, untuk aturan mainnya sebelum memasukkan jinternalframe, drag dulu JDekstopPane nya. Setting di properties dengan klik kanan pada komponen JInternalFrame nya, aktifkan icon minimize dan iconifiable. Kurang lebih inspector dan design nya seperti gambar berikut :

Setelah membuat tampilan alias design, perhatikan dalam internalframe ada layout yang harus disetting yaitu BorderLayout, tinggal klik kanan pada komponen JInternalFrame, pilih SetLayout>>>BorderLayout. Kurang lebih code nya simpel begini :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoadImage.java
 *
 * Created on Apr 23, 2012, 5:28:51 AM
 */
package freeExercise;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Yuita Arum Sari
 */
public class LoadImage extends javax.swing.JFrame {

    static ImageIcon gambar;

    /** Creates new form LoadImage */
    public LoadImage() {
        initComponents();
        this.setLocationRelativeTo(getRootPane());
    }

    /** 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() {

        chooserGambar = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Test Gambar");
        setBackground(new java.awt.Color(153, 153, 255));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/freeExercise/tambah_data40.jpg"))); // NOI18N
        jButton1.setText("BROWSE GAMBAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jInternalFrame1.setIconifiable(true);
        jInternalFrame1.setMaximizable(true);
        jInternalFrame1.setResizable(true);
        jInternalFrame1.setTitle("Test Gambar");
        jInternalFrame1.setAutoscrolls(true);
        jInternalFrame1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jInternalFrame1.setVisible(true);

        jLabel1.setText("jLabel1");
        jInternalFrame1.getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);

        jInternalFrame1.setBounds(70, 40, 490, 200);
        jDesktopPane1.add(jInternalFrame1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(220, 220, 220)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 620, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 620, 360));

        pack();
    }// </editor-fold>

    public void openGambar() {
        if (chooserGambar.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            gambar = new ImageIcon(chooserGambar.getSelectedFile().getPath());
            jLabel1.setIcon(gambar);
            jLabel1 = new JLabel(gambar);
            getContentPane().add(jLabel1);

           // panelImage.repaint();
        }
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        openGambar();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(new NimbusLookAndFeel());
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LoadImage().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JFileChooser chooserGambar;
    private javax.swing.JButton jButton1;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration
}

Kurang lebih begitulah postingan saya, tadi saya ngutek2 automatic scrolling di internalframe belum nemu reference yang pas, tolong share ya kalau teman2 ada yang bisa. Thanks ya…🙂

Salam java…

One thought on “Load Image dalam JInternalFrame

  1. terima kasih bangeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeet…..
    ne yang lagi saya cari2..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s