3 Replies - 966 Views - Last Post: 22 October 2011 - 02:48 PM Rate Topic: -----

#1 Mideoan   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 31-May 11

Displaying an image via filechooser

Posted 22 October 2011 - 02:26 PM

I'm not sure how to display the image that gets selected after it has been chosen from the fileChooser.

	private class ButtonListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				//Read the image file into an ImageIcon object
				
				JFileChooser fileChooser = new JFileChooser();
				int status = fileChooser.showOpenDialog(null);
				if (status == JFileChooser.APPROVE_OPTION)
				{
					File selectedFile = fileChooser.getSelectedFile();
					String filename = selectedFile.getPath();
					imageLabel.setIcon(filename);
				
					
					
					
					
				}
				
				
				
				//Pack the frame again to accomodate the new size of the label
				pack();
			}
		}


Is This A Good Question/Topic? 0
  • +

Replies To: Displaying an image via filechooser

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Displaying an image via filechooser

Posted 22 October 2011 - 02:43 PM

You will want to create an ImageIcon from filename, and pass the ImageIcon to the setIcon() method.
Was This Post Helpful? 1
  • +
  • -

#3 Mideoan   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 31-May 11

Re: Displaying an image via filechooser

Posted 22 October 2011 - 02:46 PM

Got it. Thanks again!

Conclusive code

	private class ButtonListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				//Read the image file into an ImageIcon object
				
				JFileChooser fileChooser = new JFileChooser();
				int status = fileChooser.showOpenDialog(null);
				if (status == JFileChooser.APPROVE_OPTION)
				{
					File selectedFile = fileChooser.getSelectedFile();
					String filename = selectedFile.getPath();
					ImageIcon image = new ImageIcon(filename);
					imageLabel.setIcon(image);
				
					
					
					
					
				}

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Displaying an image via filechooser

Posted 22 October 2011 - 02:48 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1