2 Replies - 3549 Views - Last Post: 07 January 2011 - 12:24 PM Rate Topic: -----

#1 neon612  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-October 09

Clickable JPanel

Posted 06 January 2011 - 09:24 PM

I'm working on creating a movie database using the XML feed from TheMovieDB.com (TMDB). They have links to posters and screencaps from the movies in the feeds. I want to make something like an image chooser. Where a thumbnail of each image is put into a JPanel, and I can click on the one I want to have put into the database.

I've tried my luck with it, and so far all I've been able to do is get the images setup the way I like them and register when one is clicked. The click section only prints out the image ID. I tried having the mouseClick event return the image but... nothing. I'm not entirely sure how to get this to work, or if it will. I've experimented with GUIs in java a bit before, but nothing like this. (And by experimenting, I mean a single panel/element.)

Here is the ImagePanel test class:
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;

public class ImagePanel extends JPanel implements MouseListener
{
	BufferedImage image;
	Dimension size = new Dimension();
	int ID;
	boolean useThis = false;
	
	public ImagePanel(String path, int id)
	{
		ID = id;
		image = loadImage(path);
		size.setSize(image.getWidth(), image.getHeight());
		
		addMouseListener(this);
	}
	
	protected void paintComponent(Graphics g)
	{
		// Center image in this component.
		int x = (getWidth() - size.width)/2;
		int y = (getHeight() - size.height)/2;
		g.drawImage(image, x, y, this);
	}
	
	/**
	* This method handles the communication of
	* size requirements with the parent JScrollPane.
	*/
	public Dimension getPreferredSize()
	{
		return size;
	}
	
	public BufferedImage getImage()
	{
		return image;
	}
	
	public boolean use()
	{
		return useThis;
	}
	
	private BufferedImage loadImage(String path)
	{
		BufferedImage image = null;
		
		try
		{
			image = ImageIO.read(new URL(path));
		}
		catch(Exception e)
		{
			// May as well use what is given...
			System.out.println("read error:" + e.getMessage());
		}
		
		return image;
	}
	
	public void mouseClicked(MouseEvent event)
	{
//		System.out.println(String.format("Clicked at [%d, %d]", event.getX(), event.getY()));
		System.out.println(ID);
		useThis = true;
	}
	
	public void mousePressed(MouseEvent event)
	{
	}

	public void mouseReleased(MouseEvent event)
	{
	}

	public void mouseEntered(MouseEvent event)
	{
	}

	public void mouseExited(MouseEvent event)
	{
	}
}


And here is the main Test class:
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.*;

public class ImageChooser
{
	private static URLConnection conn;
	private static String path = "";
	private static BufferedImage image = null;
	
	public static void ImageChooser(String pathStr)
	{
		path = pathStr;
	}
	
	public static void main (String args[])
	{
//		ImageChooser("9780060572969");
//		displayCover();
//	}
		String url[] = new String[12];
		url[0] = "http://hwcdn.themoviedb.org/posters/bda/4bc96ce6017a3c57fe031bda/salt-thumb.jpg";
		url[1] = "http://hwcdn.themoviedb.org/posters/688/4ce43b5e5e73d60f70000688/salt-thumb.jpg";
		url[2] = "http://hwcdn.themoviedb.org/posters/324/4c6c19897b9aa13ab9000324/salt-thumb.jpg";
		url[3] = "http://hwcdn.themoviedb.org/posters/352/4c6c18c07b9aa13ab7000352/salt-thumb.jpg";
		url[4] = "http://hwcdn.themoviedb.org/posters/343/4c6c19317b9aa13ab6000343/salt-thumb.jpg";
		url[5] = "http://hwcdn.themoviedb.org/posters/184/4bf12c5f017a3c3215000184/salt-thumb.jpg";
		url[6] = "http://hwcdn.themoviedb.org/posters/353/4d10ba657b9aa1147a000353/salt-thumb.jpg";
		url[7] = "http://hwcdn.themoviedb.org/posters/1fe/4beed41c017a3c45890001fe/salt-thumb.jpg";
		url[8] = "http://hwcdn.themoviedb.org/posters/1d7/4ca777877b9aa17aca0001d7/salt-thumb.jpg";
		url[9] = "http://hwcdn.themoviedb.org/posters/19c/4ce00f475e73d60f6d00019c/salt-thumb.jpg";
		url[10] = "http://hwcdn.themoviedb.org/posters/156/4beed3fd017a3c458a000156/salt-thumb.jpg";
		url[11] = "http://hwcdn.themoviedb.org/posters/8b4/4c6f13307b9aa13ab60008b4/salt-thumb.jpg";
		
			
	
		ImagePanel panel[] = new ImagePanel[12];
		JPanel jpanel = new JPanel();		//new GridLayout(4, 4, 5, 5));
		JFrame f = new JFrame();
		
		
		for (int i = 0; i < url.length; i++)
		{
			panel[i] = new ImagePanel(url[i], i);
			panel[i].setSize(92, panel[i].image.getHeight());
			System.out.printf("%d - (%d,%d)\n", i, panel[i].image.getWidth(), panel[i].image.getHeight());
		}
			

		for (int i = 0; i < url.length; i++)
			jpanel.add(panel[i]);
		
		jpanel.setSize(400, 700);
        	
//		JScrollPane scrollPane = new JScrollPane(panel[0]);
        	
		
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.add(jpanel);
//		f.setExtendedState(f.getExtendedState()|JFrame.MAXIMIZED_BOTH);		//Maximized window
//		f.setSize(1000,1000);
		f.setSize(panel[0].image.getWidth() * 5, panel[0].image.getHeight() * (url.length / 4 + 1));
//		f.setSize(1224, 968);
//		f.setLocation(200,200);
		f.setLocation(0,0);
		f.setVisible(true);
		
//		MouseEvent e = new MouseEvent();
		
		for(int i = 0; i < url.length; i++)
		{
//			panel[i].processMouseEvent(e);
//			System.out.println(click[0]);
			
			
			if (panel[i].use() == true)
				image = panel[i].getImage();
		}
	}
}


Edit: If there are some Linkification links in the second code block, please ignore them. I don't have them in the actual code and I don't know exactly where they are coming from.

This post has been edited by neon612: 06 January 2011 - 09:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Clickable JPanel

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10363
  • View blog
  • Posts: 38,355
  • Joined: 27-December 08

Re: Clickable JPanel

Posted 06 January 2011 - 09:43 PM

Custom painting and MouseListening isn't the best way to do this. JButtons have built in click functionality, as well as Icon support. Use JButtons, get the ImageIcon from the clicked JButton. If you store a Map<ImageIcon, Movie>, you can get the corresponding Movie object from those you create from XML (if you aren't doing this, you should) and output the data.

Also, remember to properly organize your program. Your User Interface should always be separate from your data.
Was This Post Helpful? 1
  • +
  • -

#3 neon612  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-October 09

Re: Clickable JPanel

Posted 07 January 2011 - 12:24 PM

Thank you, the button idea makes more sense now that I think about it. I'm still trying to learn how to write GUIs, maybe I should of picked a simpler one to start with.

I've changed the ImagePanel to a button with ImageIcon. I'm still at a loss as to how to get the click event to actually do something other than print out the image id number.

I've worked with python before, so I have an idea of what the Map object can do, but don't exactly know the "Java" way of doing it. And I'm not sure what you are talking about when you say "you can get the corresponding Movie object from those you create from XML (if you aren't doing this, you should)". If you mean creating a Movie class with all the information on the movie inside of it, its already been done.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1