Problem Selecting Image from file and Displaying

Problem with gettin file to show up.

Page 1 of 1

3 Replies - 954 Views - Last Post: 21 October 2010 - 08:16 PM Rate Topic: -----

#1 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 136
  • Joined: 29-March 09

Problem Selecting Image from file and Displaying

Posted 21 October 2010 - 06:05 PM

Hello, I was looking to see if anyone could help me understand a error message from following code. With this code I'm trying to simply let user select a image from file located within same folder as .java file. Then display said image on a button. Everything works up til I decide to try to display button'd image. Here is error, Thanks.

C:\Users\Yup\Desktop\JavaFiles\ImageSlicer.java:78: setText(java.lang.String) in javax.swing.AbstractButton cannot be applied to (javax.swing.ImageIcon)
imgButton.setText(new ImageIcon(myImage));

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

public class BasicFileChooser
{
	public static void main(String args[])
	{
		JFrame	f;
		f=new BasicFileChooserFrame();
		f.setVisible(true);
	}
}

class BasicFileChooserFrame extends JFrame implements ActionListener
{
JFileChooser	chooser;
JButton	chooseButton;
JButton imgButton;
Image myImage;

public BasicFileChooserFrame()
{
JPanel	mainPanel;
Container cp;

chooser	= myNewJFileChooser();

chooseButton = new JButton("Browse");
chooseButton.setActionCommand("BROWSE");
chooseButton.addActionListener(this);

imgButton = new JButton("IMG");

mainPanel = new JPanel();
mainPanel.add(chooseButton);
mainPanel.add(imgButton);

cp = getContentPane();
cp.add(mainPanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,200);
setLocation(new Point(400,300));
setVisible(true);
}

public JFileChooser myNewJFileChooser()
{
JFileChooser 	tmpChooser;
String	workingDirectory;
File	currentFile;
tmpChooser = new JFileChooser();
workingDirectory = System.getProperty("user.dir");
currentFile = new File(workingDirectory);
tmpChooser.setCurrentDirectory(currentFile);
return tmpChooser;
}
public void actionPerformed(ActionEvent e)
{
	if(e.getActionCommand().equals("BROWSE"))
		{
		doBrowse();
		}
}
public void doBrowse()
{
int response;
String getStr;
response= chooser.showOpenDialog(this);
if(response==JFileChooser.APPROVE_OPTION)
	{
	getStr=chooser.getCurrentDirectory().getPath();
	System.out.println("Trying to get Path: " + getStr);
	myImage=Toolkit.getDefaultToolkit().getImage(getStr);
	imgButton.setText(new ImageIcon(myImage));
	}
else
	System.out.println("User cancelled the dialog.");
}
}


This post has been edited by peepsrct: 21 October 2010 - 06:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem Selecting Image from file and Displaying

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Problem Selecting Image from file and Displaying

Posted 21 October 2010 - 07:20 PM

Your problem is that you're passing the ImageIcon object to the setText method with takes a string and sets the current text of that button to that String. What you should be doing is
imgButton.setIcon(new ImageIcon(myImage));.

This post has been edited by H3R3T1C: 21 October 2010 - 07:21 PM

Was This Post Helpful? 1
  • +
  • -

#3 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 136
  • Joined: 29-March 09

Re: Problem Selecting Image from file and Displaying

Posted 21 October 2010 - 07:38 PM

Thanks, that got it to compile and run. However, instead of displaying the image inside of the button it has button with "I...". Did I package the string wrong?
----
NM, Solved my problem myself. Just had to change line 75 to
getStr=chooser.getSelectedFile().getPath();

This post has been edited by peepsrct: 21 October 2010 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#4 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 136
  • Joined: 29-March 09

Re: Problem Selecting Image from file and Displaying

Posted 21 October 2010 - 08:16 PM

Now I need to write a code that uses image path, and two inputs from user(Number of Rows & Number of Columns). Then use those numbers to slice the image and re-arrange randomly.

How would be the best way to store those in a 2d-array of ImgPieces. This is the code I have for gathering a slice, I just need to adapt it to doing said process. Any input would be appreciated, Thanks!

public CropImageFrame()
	{
	JButton button;
	JPanel mainPanel;
	Image myImage;
	Image myImagePiece;
	Container cp;
	ImageProducer imProducer;
	CropImageFilter	cropFilter;
	FilteredImageSource	croppedImageProducer;

	System.out.println("Attempting to Crop Image");

	myImage=Toolkit.getDefaultToolkit().getImage("crown.jpg");
	cropFilter = new CropImageFilter(0,0,100,100);
	imProducer = myImage.getSource();
	croppedImageProducer = new FilteredImageSource(imProducer, cropFilter);
	myImagePiece = createImage(croppedImageProducer);

	button= new JButton(new ImageIcon(myImagePiece));
	mainPanel=new JPanel();
	mainPanel.add(button);
	cp=getContentPane();
	cp.add(mainPanel);
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1