Hey guys! So, created a File Chooser that gets a picture. And I want the program to display the picture on the jFrame. How can I accomplish this? I'm using NetBeans, and I'm still kinda new to it :P
Thanks guys! :D
Here's my code:
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* OpenImage.java
*
* Created on Nov 30, 2011, 11:58:57 PM
*/
/**
*
* @author Kevin
*/
public class OpenImage extends javax.swing.JFrame {
/** Creates new form OpenImage */
public OpenImage() {
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() {
jFileChooser1 = new javax.swing.JFileChooser();
jToolBar1 = new javax.swing.JToolBar();
LoadImage = new javax.swing.JButton();
jSeparator1 = new javax.swing.JToolBar.Separator();
RemoveImage = new javax.swing.JButton();
jSeparator2 = new javax.swing.JToolBar.Separator();
Exit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jToolBar1.setRollover(true);
LoadImage.setText("Load an image");
LoadImage.setFocusable(false);
LoadImage.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
LoadImage.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
LoadImage.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
LoadImageActionPerformed(evt);
}
});
jToolBar1.add(LoadImage);
jToolBar1.add(jSeparator1);
RemoveImage.setText("Remove Image");
RemoveImage.setFocusable(false);
RemoveImage.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
RemoveImage.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(RemoveImage);
jToolBar1.add(jSeparator2);
Exit.setText("Exit");
Exit.setFocusable(false);
Exit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
Exit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitActionPerformed(evt);
}
});
jToolBar1.add(Exit);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(355, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void LoadImageActionPerformed(java.awt.event.ActionEvent evt) {
FileFilter ft = new FileNameExtensionFilter("Pictures", ".jpg");
jFileChooser1.addChoosableFileFilter( ft );
int returnVal = jFileChooser1.showOpenDialog( this );
if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {
java.io.File file = jFileChooser1.getSelectedFile( );
}
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
/**
* @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(OpenImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(OpenImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(OpenImage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(OpenImage.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 OpenImage().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Exit;
private javax.swing.JButton LoadImage;
private javax.swing.JButton RemoveImage;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JToolBar.Separator jSeparator1;
private javax.swing.JToolBar.Separator jSeparator2;
private javax.swing.JToolBar jToolBar1;
// End of variables declaration
}