Skip to content

Displaying Images with JList

September 10, 2013

Objective:

–> display images by clicking the options in jlist

–> know set the listener in JList

–> know how to change images 

JAVA CODE

// import packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Ex16 extends JFrame implements ListSelectionListener
{
	// where we place the pics
	private JLabel imageJL;
	// enables us to select pics
	private JList list;
	// the options for the list
	private String[] listStr = {"pic1", "pic2", "pic3"};
	// image icon holds the pics
	private ImageIcon[] pics = {new ImageIcon("pic1.jpg"), 
				new ImageIcon("pic2.jpg"), 
				new ImageIcon("pic3.jpg")};

	public Ex16()
	{
		Container pane = getContentPane();
		pane.setLayout(null);

		list = new JList(listStr);
		// set selection one at a time
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		// if you click the list obj will do the method valueChanged
		list.addListSelectionListener(this);

		// wraps the list obj in the scroll pane
		JScrollPane selectionPane = new JScrollPane(list);

		// set imageJL obj and set default pics at first image
		imageJL = new JLabel();
		imageJL.setIcon(pics[0]);

		// set size and postion cause setLayout is null
		selectionPane.setSize(200, 40);
		imageJL.setSize(300, 280);
		selectionPane.setLocation(10, 10);
		imageJL.setLocation(10, 50);

		// adds the objs
		pane.add(selectionPane);
		pane.add(imageJL);

		// set the JFrame the appropriate values
		setVisible(true);
		setTitle("true that!!!");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(400, 400);	}
	public static void main(String[] args)
	{
		// exectes the Ex16 obj
		Ex16 example = new Ex16();
	}
	public void valueChanged(ListSelectionEvent e)
	{
		// when list obj is clicked, the index which 
		// was selected is in accordance with the index
		// of the pics obj, thus setting the appropriate
		// image
		imageJL.setIcon(pics[list.getSelectedIndex()]);
	}
}

Simple, Free Image and File Hosting at MediaFire

DOWNLOAD COMPLETE CODES HERE

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

TurtleAndRobot.com

Children's Book Reviews

Malav Shah

official Web Site

My Road to Programming

The journey of how I taught myself to program.

paritoshmishra

Look at what's brewing up guys! we all are work in progress. So don't meander, lets micromesh everything and have candid conversation about every problem related to C, C++ and JAVA. We can rejuvenate the crinkled faces.

ProgramThat;

A simple guide to programming...

ITechonology

Information Techonology

Rahul's Work

My Work Tips and Tutorial on Various Tech.

Turnip Farmers

Kingdom Scans

Gigaom

Technology news, trends and analysis covering mobile, big data, cloud, science, energy and media

The Neighborhood

society online's social conscious

HarsH ReaLiTy

A Good Blog is Hard to Find

VentureBeat

News About Tech, Money and Innovation

retireediary

The Diary of a Retiree

50 Year Project

My challenge to visit 192 countries, read 1,001 books, and watch the top 100 movies

[BTW] : Ben Trube, Writer

Writer, Programmer, Singer

My Arch Walkthrough

Arch linux made easy

Don Charisma

because anything is possible with Charisma

SutoCom Solutions

Success & Satisfaction with the Cloud

%d bloggers like this: