3 Replies - 549 Views - Last Post: 08 April 2011 - 06:20 PM Rate Topic: -----

#1 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

JFileChooser - Making it Work

Posted 08 April 2011 - 05:32 PM

Hi all,

I am sort of new at Java; just a teenager trying to get ahead. I was wondering if you could either point me in the right direction for learning how to get a file with JFileChooser and then painting it to the screen, or if you have your own home-made concoction that you think is better than all of the rest ;).

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: JFileChooser - Making it Work

#2 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: JFileChooser - Making it Work

Posted 08 April 2011 - 05:58 PM

I'm not sure if this is how it's supposed to be done, but here's how I use JFileChooser:
JFileChooser fc = new JFileChooser(".");
int returnVal = fc.showOpenDialog(gFrame);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
  File myFile = fc.getSelectedFile();
  //Do stuff with file
}


Passing JFileChooser "." sets the directory the program is run from as the directory the open file dialogue starts at. Otherwise, passing it nothing makes the dialogue start from the users root directory, Like MyDocuments on my Windows PC.

You can use fc.showSaveDialog() instead to write data to a file.
Was This Post Helpful? 1
  • +
  • -

#3 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: JFileChooser - Making it Work

Posted 08 April 2011 - 06:04 PM

Well, I know that already, see the code below, but I really can't seem to understand if I am doing the ocrrect thing with the file so it can be painted to the screen (frame).

Please note that FCL is a JButton, PictureChooser is a JFileChooser, and Picture_File is a null file while NewImage1 is a BufferedImage.

FCL.addActionListener(
			new ActionListener()
                         {
				public void actionPerformed(ActionEvent e)
				{
                                 
				PictureChooser.showOpenDialog(GuiCreation.frame);

                                int actionTaken = PictureChooser.showOpenDialog(GuiCreation.frame);

                                if (actionTaken == PictureChooser.APPROVE_OPTION)
                                {
                                
                                Picture_File = PictureChooser.getSelectedFile();

                                    try
                                    {
                                        NewImage1 = ImageIO.read(Picture_File);
                                    }
                                    catch (IOException E)
                                    {
                                        JOptionPane.showMessageDialog(GuiCreation.frame,
                                          "The file was not found",
                                          "File Not Found",
                                           JOptionPane.ERROR_MESSAGE);
                                    }
                                    finally
                                    {
                                        System.exit(0);
                                    
                                    }

                                
				}
                                else if (actionTaken == PictureChooser.CANCEL_OPTION)
                                    {
                                        System.exit(0);
                                    }
                             }
			}
		);

Was This Post Helpful? 0
  • +
  • -

#4 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: JFileChooser - Making it Work

Posted 08 April 2011 - 06:20 PM

I'm assuming NewImage1 is private member. If you're just drawing it to the frame, overload the paint(Graphics g) function to draw NewImage1.
public void paint(Graphics g)
{
  super.paint(g);
  if(NewImage1 != null)
  g.drawImage(NewImage1,xPos,yPos,null);
}


Then call repaint() after you load the image.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1