Hi guys.
I decided to post this simple Photo viewer application.
Its strictly for beginners, and cab be improved.

     * @author Neon Tetras
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;

    public class ImageViewer extends javax.swing.JFrame {

         * Class constructor: ImageViewer
        public ImageViewer() {

            listFiles(Path);    //Lists all the files in the directory on window opening

            setLabelIcon(Path,filenames[position]); //sets the label to display the first
                                                    //image in the directory on window opening.
         *Initialize components
        private void initComponents() {
            setTitle("Image Viewer");
            setLayout(new java.awt.BorderLayout());// The layout is BorderLayout

            picLabel = new javax.swing.JLabel();        //Create the Label to display the picture

            PreviousButton = new javax.swing.JButton();
            PreviousButton.setIconTextGap(10); //Distance between the icon and text is 10

            PreviousButton.addActionListener(new java.awt.event.ActionListener() { //Register an actionListener for the PreviousButton
                public void actionPerformed(java.awt.event.ActionEvent evt) {

            NextButton = new javax.swing.JButton();
            NextButton.setIconTextGap(10); //Distance between the icon and text is 10

            NextButton.addActionListener(new java.awt.event.ActionListener() {  //Registers an actionListener for the NextButton
                public void actionPerformed(java.awt.event.ActionEvent evt) {

            javax.swing.Box vbox = javax.swing.Box.createVerticalBox(); //VerticalBox to hold the pictureLabel and the buttons


            javax.swing.JPanel prev_next_pane = new javax.swing.JPanel(); //Panel to hold the Previous and Next buttons.

            java.awt.FlowLayout flow = new java.awt.FlowLayout(java.awt.FlowLayout.CENTER);


            vbox.add(prev_next_pane); //Add the panel to the verticalBox

        java.awt.Toolkit theKit = getToolkit();
        java.awt.Dimension size = theKit.getScreenSize();

        setMinimumSize(new java.awt.Dimension(900,600));
        //setMaximumSize(new Dimension(size.width/4 +  50,size.height/4));


         *Handler for previous button
        private void PreviousButtonActionPerformed(java.awt.event.ActionEvent evt) {
          position--;  //Decrement the index position of the array of filenames  by one on buttonPressed
          if(!NextButton.isEnabled()){      //if NextButton is
              NextButton.setEnabled(true);  //disabled, enable it
          if(position == 0){                    //If we are viewing the first Picture in 
              PreviousButton.setEnabled(false); //the directory, disable previous button
        }//End of PreviousButton handler

         *Handler for NextButton
        private void NextButtonActionPerformed(java.awt.event.ActionEvent evt) {

          position++; //Increment the index position of array of filenames  by one on buttonPressed

            if(position == filenames.length){
                position --;


        }//End of NextButton handler

         * @param args the command line arguments
        public static void main(String args[]) {
             * Set the Nimbus look and feel
             * 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())) {
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(ImageViewer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

             * Create and display the form
            java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    new ImageViewer().setVisible(true);

        /**Method to list all the files in the directory
         *And store their names in an array
        private void listFiles(String Path){
        File file = new File(Path);
        filenames = file.list(); //store the file names in the array of strings.

        for(String names : filenames){
        System.out.println(names); //Print the filenames to the console just so you can see 

        //Method to set the picture on the label
         private void setLabelIcon(String Path,String name){
         File file = new File(Path+"\\"+name);
        java.awt.image.BufferedImage image = null;

        image = ImageIO.read(file); //Reas the image from the file.
         }catch(IOException ie){
        javax.swing.JOptionPane.showMessageDialog(this,"Error reading image file","Error",
        ImageIcon icon = new ImageIcon(image);
        int width = 600;
        int height = 400;
        Image img = icon.getImage().getScaledInstance(width,height,java.awt.Image.SCALE_SMOOTH); //Images produced will remain a fixed size, 600 * 400

        ImageIcon newIcon = new ImageIcon(img); //Create a new imageicon from an image object.

        //Now we want to create a caption for the pictures using their file names
        String pictureName = file.getName();
        int pos = pictureName.lastIndexOf(".");        //This removes the extensions 
       String caption = pictureName.substring(0,pos); //from the file names. e.g .gif, .jpg, .png
       picLabel.setIcon(newIcon);                   //Set the imageIcon on the Label
       picLabel.setText(caption);                   //Set the caption
       picLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); //Caption appears below the image

         //Variables declaration
        int position = 0; //Initial position is 0
        String[] filenames; //Array to hold the file names
        final String Path = System.getProperty("user.home") +"\\User_Name\\pictures\\"; //path to your images
        private javax.swing.JButton NextButton;
        private javax.swing.JButton PreviousButton;
        private javax.swing.JLabel picLabel;
        // End of variables declaration//GEN-END:variables
    }//End of class
Begginnerdev commented: Nice post! +9
JamesCherrill commented: Good example of Java perogramming. Well done +15

Its strictly for beginners, and can be improved.

You are being too modest. I would be perfectly happy to have written that myself. Well done.

you could use a bit more import statements, rather than using full names for your variables, and applying the same naming convention on your JButtons as on your other variables might make it easier to see in your code what is a variable and what a class of which you call a static method.

but over all, indeed not too bad.

Those fully-qualified swing class names look like GUI builder generated code - so no problem IMHO.
But I agree the variables names should conform to the normal Java standards. (And perfect indentation would help readability.)

true about the fully-qualified names, but then again, linking them to the NetBeans UI builder is why I usually don't like to see them ;)

and add

  • newIcon.flush() before is a new ImageIcon / Icon added to JLabel()
  • flush() can protect JLabels resouces to repaint correctly, especially in the cased that ImageIcon / Icon is loaded from local/network HDD or from URL (web etc...)

i started this project using netbeans. that is why i had these fully qualified names

Definitely a nice one. For me, I may only able to develop these kind of application in the environment of .NET with the help of some UI develop toolkit. But, you do achieve it with such patience.

Uchiha: the netbeans ide, which the op used, also provides drag-n-drop creation of gui's.