cretaros 0 Junior Poster in Training

Help me in correctinf the errror i encounter here, i want the image file i have selected using Jfilechooser to be set on the image panel.

i tried this but it failed: picthandler.setImageIcon(file);

Here is the class Code , i have commented exaclty whr i want the bug to be fixed [

   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import javax.swing.event.*;
   import java.lang.Object.*;
   import javax.swing.border.*;
   import java.awt.Toolkit;
   import java.awt.Cursor;
   import java.awt.event.MouseEvent;
   import java.awt.event.MouseAdapter;
   import java.awt.Cursor;   
   import java.text.*;
   import java.sql.*;
   import java.lang.Object.*;
   import java.awt.Image.*;
   import java.awt.Image;
   import javax.swing.ImageIcon;
   import java.awt.Color;
   import java.awt.Dimension;
   import java.awt.Toolkit;
   import java.util.*;
   import java.text.*;
   import java.math.*;
   import javax.swing.filechooser.*;
   import java.io.*;
   import java.io.File;

   import javax.swing.SwingUtilities;
   import javax.swing.filechooser.FileFilter;

    public class addOfficer extends JFrame 
   {
      private JTextField fnametxt;
      private JTextField surnametxt;
      private JTextField dobtxt;
      private JTextField addresstxt;
      private JLabel fnamelbl;
      private JLabel snamelbl;
      private JLabel doblbl;
      private JLabel addresslbl;
      private JLabel qualificationlbl;
      private JPanel picthandler;
      private JRadioButton malerbtn;
      private JRadioButton femalerbtn;
      private JButton b;
      private JLabel idlbl;
      private JTextField sntxt;
      private JSeparator line1;
      private JButton bckbtn;
      private JButton clearbtn;
      private JButton open;
      private JButton exitbtn;
      private JButton savebtn;
      private JButton logoutbtn;
      private JLabel datelbl;
      private JTextField datetxt;
      private JTextField qualificationtxt;
      private JComboBox rankbox;
      private JSeparator line2;
      private JComboBox smokerbox;
      private JComboBox marital;
      private JFileChooser fc;
      private Image img;
      static private final String newline = "\n";




       public addOfficer()
      {
         MouseHandler mousehandler = new MouseHandler();

         try

         {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
         }
             catch (Exception ex)
            {
               JOptionPane.showMessageDialog(null,"Failed loading L&F: ");
               JOptionPane.showMessageDialog(null,ex);
            }

        //construct preComponents
         String[] rankboxItems = {"------------Rank-------------", "Warder", "Sergent", "Sub-Inspector", "Inspector", "A/Supritendant", "Supritendant", "S/Supritendant", "A/ACP", "ACP", "S/ACP", "D/CP", "SD/CP", "Comissioner"};
         String[] smokerboxItems = {"----------Smoking-----------", "Smoker", "Non-Smoker"};
         String[] maritalItems = {"--------Marital Status--------", "Single", "Married", "Divorced"};

        //construct components
       //  frontpanel = new JPanel();
        // frontpanel.setLayout(null);
        // frontpanel.setBorder(BorderFactory.createLoweredBevelBorder());
      //    setBorder(new TitledBorder("ORPHAN REGISTRATION SYSTEM"));
         //add (frontpanel);
         //frontpanel.setBounds (40, 150, 300 ,310);

         fnametxt = new JTextField (5);
         surnametxt = new JTextField (5);
         dobtxt = new JTextField (5);
         addresstxt = new JTextField (5);

         fnamelbl = new JLabel ("Firstname :");
         fnamelbl.setForeground(Color.black);
         fnamelbl.setFont(new Font ("Tahoma",Font.BOLD,12));

         snamelbl = new JLabel ("Surname :");
         snamelbl.setForeground(Color.black);
         snamelbl.setFont(new Font ("Tahoma",Font.BOLD,12));

         doblbl = new JLabel ("D.O.B :");
         doblbl.setForeground(Color.black);
         doblbl.setFont(new Font ("Tahoma",Font.BOLD,12));

         addresslbl = new JLabel ("Address :");
         addresslbl.setForeground(Color.black);
         addresslbl.setFont(new Font ("Tahoma",Font.BOLD,12));

         qualificationlbl = new JLabel ("Qualification:");
         qualificationlbl.setForeground(Color.black);
         qualificationlbl.setFont(new Font ("Tahoma",Font.BOLD,12));

         picthandler = new JPanel ();

         malerbtn = new JRadioButton ("Male");
         malerbtn.setForeground(Color.black);
         malerbtn.setFont(new Font ("Tahoma",Font.BOLD,10));

         femalerbtn = new JRadioButton ("Female");
         femalerbtn.setForeground(Color.black);
         femalerbtn.setFont(new Font ("Tahoma",Font.BOLD,10));

         b = new JButton ("Gender");

         idlbl = new JLabel ("Service No :");
         idlbl.setForeground(Color.black);
         idlbl.setFont(new Font ("Tahoma",Font.BOLD,12));

         sntxt = new JTextField (5);
         line1 = new JSeparator ();


         bckbtn = new JButton ("Back");
         bckbtn.addMouseListener(mousehandler);
         bckbtn.setForeground(Color.black);
         bckbtn.setFont(new Font ("Tahoma",Font.BOLD,12));
         bckbtn.setIcon(new ImageIcon("icons/back.png")); 

         clearbtn = new JButton ("Clear");
         clearbtn.addMouseListener(mousehandler);
         clearbtn.setForeground(Color.black);
         clearbtn.setFont(new Font ("Tahoma",Font.BOLD,12));
         clearbtn.setIcon(new ImageIcon("icons/clear.png")); 


         exitbtn = new JButton ("Exit");
         exitbtn.addMouseListener(mousehandler);
         exitbtn.setForeground(Color.black);
         exitbtn.setFont(new Font ("Tahoma",Font.BOLD,12));
         exitbtn.setIcon(new ImageIcon("icons/Exit.png")); 

         open  = new JButton ("Load Photo");
         open.addMouseListener(mousehandler);
         open.setForeground(Color.black);
         open.setFont(new Font ("Tahoma",Font.BOLD,12));
         open.setIcon(new ImageIcon("icons/open.png")); 


         savebtn = new JButton ("Save");
         savebtn.addMouseListener(mousehandler);
         savebtn.setForeground(Color.black);
         savebtn.setFont(new Font ("Tahoma",Font.BOLD,12));
         savebtn.setIcon(new ImageIcon("icons/save.png"));

         logoutbtn = new JButton ("Logout");
         logoutbtn.addMouseListener(mousehandler);
         logoutbtn.setForeground(Color.black);
         logoutbtn.setFont(new Font ("Tahoma",Font.BOLD,12));
         logoutbtn.setIcon(new ImageIcon("icons/lock.png")); 


         datelbl = new JLabel ("Date Entered :");
         datelbl.setForeground(Color.black);
         datelbl.setFont(new Font ("Tahoma",Font.BOLD,12));

         datetxt = new JTextField (5);
         qualificationtxt = new JTextField (5);

         rankbox = new JComboBox (rankboxItems);
         rankbox.addMouseListener(mousehandler);
         rankbox.setForeground(Color.black);
         rankbox.setFont(new Font ("Tahoma",Font.BOLD,10));

         line2 = new JSeparator ();

         fc = new JFileChooser(  );

         smokerbox = new JComboBox (smokerboxItems);
         smokerbox.addMouseListener(mousehandler);
         smokerbox.setForeground(Color.black);
         smokerbox.setFont(new Font ("Tahoma",Font.BOLD,10));

         marital = new JComboBox (maritalItems);
         marital.addMouseListener(mousehandler);
         marital.setForeground(Color.black);
         marital.setFont(new Font ("Tahoma",Font.BOLD,10));

         JLabel footer = new JLabel();
         footer.setIcon(new ImageIcon("icons/header.png"));
         add(footer);
         footer.setBounds(-50,580,800,200);

         JLabel header = new JLabel();
         header.setIcon(new ImageIcon("icons/header3.png"));
         add(header);
         header.setBounds(-50,-130,800,200);



        //set components properties
         fnametxt.setToolTipText ("Enter Officer Firstname");
         surnametxt.setToolTipText ("Enter Officer Surname");
         dobtxt.setToolTipText ("Enter Officer Date of Birth");
         addresstxt.setToolTipText ("Enter  Officer Address");
         picthandler.setToolTipText ("Handles pictures");
         malerbtn.setToolTipText ("Tick if Officer is male");
         femalerbtn.setToolTipText ("Tick if Officer is female");
         b.setEnabled (false);
         b.setToolTipText ("Select Gender");
         sntxt.setToolTipText ("Enter Officer service No");
         bckbtn.setToolTipText ("Back to main Window");
         clearbtn.setToolTipText ("Reset All data fields");
         exitbtn.setToolTipText ("Quit PMS system");
         open.setToolTipText ("Load a Photo of an Officer");

         savebtn.setToolTipText ("Save Officers details ");
         logoutbtn.setToolTipText ("Logout system");
         datetxt.setToolTipText ("Click to enter Current Date");
         qualificationtxt.setToolTipText ("Enter Officer qualification");
         rankbox.setToolTipText ("Select Rank");
         smokerbox.setToolTipText ("Select if Officer is a Smoker");

        //adjust size and set layout
         setIconImage (getToolkit().getImage ("icons/logo.png"));
         //getContentPane(). setBackground(Color.LIGHT_GRAY);
         setTitle("Prison Management System");
         setPreferredSize (new Dimension (748, 654));
         setLayout (null);

        //add components
         add (fnametxt);
         add (open);
         add (surnametxt);
         add (dobtxt);
         add (addresstxt);
         add (fnamelbl);
         add (snamelbl);
         add (doblbl);
         add (addresslbl);
         add (qualificationlbl);
         add (picthandler);
         add (malerbtn);
         add (femalerbtn);
         add (b);
         add (idlbl);
         add (sntxt);
         add (line1);
         add (bckbtn);
         add (clearbtn);
         add (exitbtn);
         add (savebtn);
         add (logoutbtn);
         add (datelbl);
         add (datetxt);
         add (qualificationtxt);
         add (rankbox);
         add (line2);
         add (smokerbox);
         add (marital);

        //set component bounds (only needed by Absolute Positioning)
         fnametxt.setBounds (140, 115, 160, 20);
         surnametxt.setBounds (140, 145, 160, 20);
         dobtxt.setBounds (140, 175, 160, 20);
         addresstxt.setBounds (140, 205, 160, 20);
         fnamelbl.setBounds (40, 115, 80, 20);
         snamelbl.setBounds (40, 140, 80, 25);
         doblbl.setBounds (40, 175, 60, 20);
         addresslbl.setBounds (40, 200, 70, 25);
         qualificationlbl.setBounds (40, 235, 80, 20);
         picthandler.setBounds (410, 75, 250, 170);
         malerbtn.setBounds (140, 395, 65, 20);
         femalerbtn.setBounds (220, 395, 75, 20);
         b.setBounds (115, 340, 205, 65);
         idlbl.setBounds (40, 290, 100, 25);
         sntxt.setBounds (140, 295, 160, 20);
         line1.setBounds (-10, 535, 755, 15);
         bckbtn.setBounds (40, 565, 100, 25);
         clearbtn.setBounds (495, 565, 105, 25);
         exitbtn.setBounds (620, 565, 100, 25);
         savebtn.setBounds (330, 555, 125, 35);
         logoutbtn.setBounds (195, 560, 100, 25);
         datelbl.setBounds (40, 260, 95, 25);
         datetxt.setBounds (140, 265, 160, 20);
         qualificationtxt.setBounds (140, 235, 160, 20);
         rankbox.setBounds (485, 310, 170, 25);
         line2.setBounds (-10, 50, 755, 35);
         smokerbox.setBounds (485, 360, 170, 25);
         marital.setBounds (485, 410, 170, 25);
         open. setBounds (410, 255, 250, 20);


         datetxt.addFocusListener(new FocusHandler());

        // Add action handling tio buttons & combo's
         ButtonHandler Handler = new ButtonHandler();
         open.addActionListener(Handler);


      }
       class FocusHandler implements FocusListener
      {
         java.util.Date today=new java.util.Date();
         DateFormat format=DateFormat.getDateInstance();
         String formatted=format.format(today);


          public void focusGained(FocusEvent e)
         {
            Object obj=(JTextField)e.getSource();
            if(obj==datetxt)
            {
               datetxt.setText(formatted);
            }

         }
          public void focusLost(FocusEvent e)
         {
         }


      }
       public class ButtonHandler implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
         {
            if(e.getSource() == open)
            {
               int returnVal = fc.showOpenDialog(addOfficer.this);

               if (returnVal == JFileChooser.APPROVE_OPTION)
               {
             //This is where a real application would open the file.
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                  File file = fc.getSelectedFile();


                  picthandler.setImageIcon(file);
   //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


                        JOptionPane.showMessageDialog(null,
                     "Photo Loaded Susccesfully!",
                     "PMS Notifications",
                     JOptionPane.INFORMATION_MESSAGE);
               } 
               else {

                  JOptionPane.showMessageDialog(null,
                     "Open command cancelled by user!",
                     "PMS Notifications",
                     JOptionPane.INFORMATION_MESSAGE);

               }
              // picthandler.setCaretPosition(picthandler.getDocument().getLength());


            }

         }
      }
    /** Returns an ImageIcon, or null if the path was invalid. */
       protected static ImageIcon createImageIcon(String path) 
      {
         java.net.URL imgURL = addOfficer.class.getResource(path);
         if (imgURL != null) 
         {
            return new ImageIcon(imgURL);
         } 
         else
         {
            JOptionPane.showMessageDialog(null,"Couldn't find file: " + path);
            return null;
         }
      }
       /*
        *     ===========================================
         *       MAIN FUNCTION OF THE ADD OFFICER CLASS
         *     ===========================================
         */
       public static void main (String[] args) 
      {

         addOfficer frame = new addOfficer ();
         frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
         frame.pack(); 
         frame.setResizable(false);
         frame.setLocationRelativeTo(null);
         frame.setVisible (true);

         frame.addWindowListener(
                new WindowAdapter()
               {
                   public void windowClosing(WindowEvent we)
                  {
                     if (JOptionPane.showConfirmDialog(new JFrame(),
                     "Are you sure you want to quit the PMS System ?", 
                     "PMS Exit Confirmation",
                     JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                        System.exit(0);



                  }
               });


      }
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.