Can't add image to JPanel using JFilechooser
807601Apr 4 2008 — edited Apr 5 2008Hi,
This is my first project in NetBeans and I am trying to pick an image from a file and add it to a Jpanel. The picture is not showing up on the panel and I'm not sure where to go from here to fix it. Here is my code. Thanks in advance.
/*
* JFrame.java
*
* Created on 04 April 2008, 10:07
*/
package newpanel;
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
public class JFrame extends javax.swing.JFrame {
private JFileChooser jFileChooser1;
private File theImageFile;
/** Creates new form JFrame */
public JFrame() {
initComponents();
}
private void initComponents() {
panelForImage = new javax.swing.JPanel();
previous = new javax.swing.JButton();
next = new javax.swing.JButton();
nameOfMenuBar = new javax.swing.JMenuBar();
nameOfMenu = new javax.swing.JMenu();
goToImages = new javax.swing.JMenuItem();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Frame for album");
panelForImage.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
javax.swing.GroupLayout panelForImageLayout = new javax.swing.GroupLayout(panelForImage);
panelForImage.setLayout(panelForImageLayout);
panelForImageLayout.setHorizontalGroup(
panelForImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 198, Short.MAX_VALUE)
);
panelForImageLayout.setVerticalGroup(
panelForImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 116, Short.MAX_VALUE)
);
previous.setText("Previous");
next.setText("Next");
nameOfMenu.setText("Menu");
goToImages.setText("Item");
goToImages.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
goToImagesActionPerformed(evt);
}
});
nameOfMenu.add(goToImages);
nameOfMenuBar.add(nameOfMenu);
jMenu1.setText("File");
nameOfMenuBar.add(jMenu1);
jMenu2.setText("Edit");
nameOfMenuBar.add(jMenu2);
setJMenuBar(nameOfMenuBar);
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(84, 84, 84)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(previous)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(next))
.addComponent(panelForImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(114, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(panelForImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(previous)
.addComponent(next))
.addContainerGap(74, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void goToImagesActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
File file = new File ("./photos");
jFileChooser1 = new JFileChooser(file);
jFileChooser1.addChoosableFileFilter(new ImageFileFilter());
int returnVal = jFileChooser1.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
theImageFile = jFileChooser1.getSelectedFile();
Container c = this.getContentPane();
final JLabel label = new JLabel(new ImageIcon("theImageFile.getPath()"));
c.add(label);
label.revalidate(); // required because you are adding a component after the GUI is visible
setVisible(true);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem goToImages;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu nameOfMenu;
private javax.swing.JMenuBar nameOfMenuBar;
private javax.swing.JButton next;
private javax.swing.JPanel panelForImage;
private javax.swing.JButton previous;
// End of variables declaration
}
//Add a class to filter the files
package newpanel;
import java.io.File;
public class ImageFileFilter extends
javax.swing.filechooser.FileFilter
{
public boolean accept(File f)
{
//if it is a directory -- show it so return true.
if (f.isDirectory())
return true;
//get the extension of the file
String extension = getExtension(f);
//check to see if the extension is equal to "jpg" or "gif"
if ((extension.equals("jpg")) || (extension.equals("gif")))
return true;
//default -- fall through. False is return on all
//occasions except:
//a) the file is a directory
//b) the file's extension is what we are looking for.
return false;
}
/**
Again, this is declared in the abstract class The description of this filter
*/
public String getDescription()
{
return "jpg/gif files";
}
/**
Method to get the extension of the file, in lowercase
*/
private String getExtension(File f)
{
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1)
return s.substring(i+1).toLowerCase();
return "";
}
}