11 Replies - 7412 Views - Last Post: 09 September 2009 - 08:13 PM Rate Topic: -----

#1 fordgirl29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 06-March 09

Java Image Viewer

Posted 09 September 2009 - 04:36 PM

On this program I must display a file chooser, which I have it doing that. It will not load an image, also I keep getting this message every time I compile the code, this is what the message is:

Note: E:\ITP 220\Chapter 13\Image Viewer\myImages.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class myImages extends JFrame
{
	private JPanel imagePanel;		//Holds the label
	private JPanel buttonPanel;	//Holds the button
	private JLabel imageLabel;		//Displays the image
	private JButton button;			//Gets the image
	
	public myImages()
	{
		setTitle("My Images");			//Setting the title 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);			//Setting the action close to the close button
		setLayout(new BorderLayout());			//Creating the border
		buildImagePanel();		//builds the image panel
		buildButtonPanel();		//builds the button panel
		add(imagePanel, BorderLayout.CENTER);		//Centers the image on the panel
		add(buttonPanel, BorderLayout.SOUTH);		//Puts the button at the bottom of the panel
		pack();		//Packs the image to fit the panel
		setVisible(true);		//Sets the visibility to true, so the image will display
	}//end myImages
	
	private void buildImagePanel()
	{
		imagePanel = new JPanel();			//Creates a new panel
		imageLabel = new JLabel();		//Creates a label to display text
		imagePanel.add(imageLabel);		//Adds the label to the panel
	}//end buildImagePanel
	
	private void buildButtonPanel()
	{
		buttonPanel = new JPanel();		//Creates a button
		button = new JButton("Choose image to be displayed");		//Displays text on the button
		button.addActionListener(new ButtonListener());			//Sets an action listener for the button
		buttonPanel.add(button);			//Adds the button to the panel
	}//end buildButtonPanel
	
	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{			
			JFileChooser fileChooser = new JFileChooser();
			int status = fileChooser.showOpenDialog(null);
				if(status == JFileChooser.APPROVE_OPTION)
				{
					File selectedFile = fileChooser.getSelectedFile();
					String filename = selectedFile.getPath();
				}//end of if statement
			
			ImageIcon selectedFile = new ImageIcon();		//Puts the image into the ImageIcon object
			imageLabel.setIcon(selectedFile);		//Displays the image in the label
			imageLabel.setText(null);		//Removes any text from the label
			pack();		//Pack the frame so the image will fit the new size

		}//end actionPerformed				
	}//end buttonlistener class
	
	public static void main(String[] args)
	{
		myImages image = new myImages();
		image.show();
	}//end of main class
}//end class myImages


This post has been edited by fordgirl29: 09 September 2009 - 04:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Image Viewer

#2 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Java Image Viewer

Posted 09 September 2009 - 04:54 PM

image.show(); that is where your error is coming from. show is deprecated for setVisible(true).

I don't see why its needed seeing as how you set it do visible in the constructor.

Edit: shouldn't be an error, just a warning.

This post has been edited by syfran: 09 September 2009 - 04:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 fordgirl29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 06-March 09

Re: Java Image Viewer

Posted 09 September 2009 - 05:02 PM

View Postsyfran, on 9 Sep, 2009 - 03:54 PM, said:

image.show(); that is where your error is coming from. show is deprecated for setVisible(true).

I don't see why its needed seeing as how you set it do visible in the constructor.

Edit: shouldn't be an error, just a warning.


Thank you for that bit of information. But I still can't get the image to display.

This post has been edited by fordgirl29: 09 September 2009 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Image Viewer

Posted 09 September 2009 - 05:48 PM

ImageIcon selectedFile = new ImageIcon(); //Puts the image into the ImageIcon object

you have to pass the filename as parameter to new ImageIcon();
Was This Post Helpful? 0
  • +
  • -

#5 fordgirl29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 06-March 09

Re: Java Image Viewer

Posted 09 September 2009 - 06:47 PM

View Postpbl, on 9 Sep, 2009 - 04:48 PM, said:

ImageIcon selectedFile = new ImageIcon(); //Puts the image into the ImageIcon object

you have to pass the filename as parameter to new ImageIcon();



Ok but I don't have a specific file to pass as a parameter. The user chooses the image their selves from what ever images they have on their own computer.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Image Viewer

Posted 09 September 2009 - 06:53 PM

If you are using a FileChooser is to get a file no ?
			JFileChooser fileChooser = new JFileChooser();
			int status = fileChooser.showOpenDialog(null);
				if(status == JFileChooser.APPROVE_OPTION)
				{
					File selectedFile = fileChooser.getSelectedFile();
					String filename = selectedFile.getPath();
					ImageIcon selectedFile = new ImageIcon(filename);		//Puts the image into the ImageIcon object
					imageLabel.setIcon(selectedFile);		//Displays the image in the label
					imageLabel.setText(null);		//Removes any text from the label
					pack();		//Pack the frame so the image will fit the new size
				}
 

Was This Post Helpful? 0
  • +
  • -

#7 fordgirl29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 06-March 09

Re: Java Image Viewer

Posted 09 September 2009 - 07:10 PM

View Postpbl, on 9 Sep, 2009 - 05:53 PM, said:

If you are using a FileChooser is to get a file no ?
			JFileChooser fileChooser = new JFileChooser();
			int status = fileChooser.showOpenDialog(null);
				if(status == JFileChooser.APPROVE_OPTION)
				{
					File selectedFile = fileChooser.getSelectedFile();
					String filename = selectedFile.getPath();
					ImageIcon selectedFile = new ImageIcon(filename);		//Puts the image into the ImageIcon object
					imageLabel.setIcon(selectedFile);		//Displays the image in the label
					imageLabel.setText(null);		//Removes any text from the label
					pack();		//Pack the frame so the image will fit the new size
				}
 


Yes a file chooser is to let you choose a file. I tried passing filename as a parameter
ImageIcon selectedFile = new ImageIcon(filename);

But I get an error message saying that selectedFile is already defined in actionPerformed. I know i'm over thinking this problem.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Image Viewer

Posted 09 September 2009 - 07:22 PM

Yes you will have to make up your mind... didn't realized it when I cut & poaste your code

File selectedFile = fileChooser.getSelectedFile();
String filename = selectedFile.getPath();
ImageIcon selectedFile = new ImageIcon(filename);	   



is the variable selectedFile is a File or an ImageIcon ?
may be you need 2 different variable names their
Was This Post Helpful? 0
  • +
  • -

#9 fordgirl29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 06-March 09

Re: Java Image Viewer

Posted 09 September 2009 - 07:39 PM

View Postpbl, on 9 Sep, 2009 - 06:22 PM, said:

Yes you will have to make up your mind... didn't realized it when I cut & poaste your code

File selectedFile = fileChooser.getSelectedFile();
String filename = selectedFile.getPath();
ImageIcon selectedFile = new ImageIcon(filename);	   



is the variable selectedFile is a File or an ImageIcon ?
may be you need 2 different variable names their


The variable selectedFile is suppose to be an ImageIcon. This code
File selectedFile = fileChooser.getSelectedFile();
String filename = selectedFile.getPath();
ImageIcon selectedFile = new ImageIcon(filename);


I got this code right out of the book, I changed a couple of variable names to work with my code.
This is what the problem says to do
Write an application that allows the user to view image files. The application should use either a button or a menu item that displays a file chooser. When the user selects an image file, it should be loaded and displayed.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Image Viewer

Posted 09 September 2009 - 07:44 PM

View Postfordgirl29, on 9 Sep, 2009 - 06:39 PM, said:

The variable selectedFile is suppose to be an ImageIcon. This code
File selectedFile = fileChooser.getSelectedFile();
String filename = selectedFile.getPath();
ImageIcon selectedFile = new ImageIcon(filename);


I got this code right out of the book, I changed a couple of variable names to work with my code.
This is what the problem says to do
Write an application that allows the user to view image files. The application should use either a button or a menu item that displays a file chooser. When the user selects an image file, it should be loaded and displayed.

So if you insist that selectedFile is an ImageIcon give another name to the File object ???????
Was This Post Helpful? 1
  • +
  • -

#11 fordgirl29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 06-March 09

Re: Java Image Viewer

Posted 09 September 2009 - 07:44 PM

View Postfordgirl29, on 9 Sep, 2009 - 06:39 PM, said:

View Postpbl, on 9 Sep, 2009 - 06:22 PM, said:

Yes you will have to make up your mind... didn't realized it when I cut & poaste your code

File selectedFile = fileChooser.getSelectedFile();
String filename = selectedFile.getPath();
ImageIcon selectedFile = new ImageIcon(filename);	   



is the variable selectedFile is a File or an ImageIcon ?
may be you need 2 different variable names their


The variable selectedFile is suppose to be an ImageIcon. This code
File selectedFile = fileChooser.getSelectedFile();
String filename = selectedFile.getPath();
ImageIcon selectedFile = new ImageIcon(filename);


I got this code right out of the book, I changed a couple of variable names to work with my code.
This is what the problem says to do
Write an application that allows the user to view image files. The application should use either a button or a menu item that displays a file chooser. When the user selects an image file, it should be loaded and displayed.



I would like to thank everyone for your help. I got this program figured out thanks to all of you that helped me.
Thank you Thank you Thank you
Was This Post Helpful? 0
  • +
  • -

#12 LynnL  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 109
  • Joined: 13-April 09

Re: Java Image Viewer

Posted 09 September 2009 - 08:13 PM

View Postfordgirl29, on 9 Sep, 2009 - 06:44 PM, said:

View Postfordgirl29, on 9 Sep, 2009 - 06:39 PM, said:

View Postpbl, on 9 Sep, 2009 - 06:22 PM, said:

Yes you will have to make up your mind... didn't realized it when I cut & poaste your code

File selectedFile = fileChooser.getSelectedFile();
String filename = selectedFile.getPath();
ImageIcon selectedFile = new ImageIcon(filename);	   



is the variable selectedFile is a File or an ImageIcon ?
may be you need 2 different variable names their


The variable selectedFile is suppose to be an ImageIcon. This code
File selectedFile = fileChooser.getSelectedFile();
String filename = selectedFile.getPath();
ImageIcon selectedFile = new ImageIcon(filename);


I got this code right out of the book, I changed a couple of variable names to work with my code.
This is what the problem says to do
Write an application that allows the user to view image files. The application should use either a button or a menu item that displays a file chooser. When the user selects an image file, it should be loaded and displayed.



I would like to thank everyone for your help. I got this program figured out thanks to all of you that helped me.
Thank you Thank you Thank you


Hope you clicked the good This Post was Helpful ?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1