VB icon

Scrolling Image Viewer

Submitted on: 6/20/2017 9:10:23 PM
By: Leo 
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 19
     Basic scroll image viewer. Adding filter (select images files only).
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
// Name: Scrolling Image Viewer
// Description:Basic scroll image viewer.
Adding filter (select images files only).
// By: Leo

import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import java.io.File;
public class TutorialImageViewer extends JFrame {
	public TutorialImageViewer() {
		this.setTitle("Image Viewer");
		this.setSize(240, 100);
		JButton openButton = new JButton("Open Image");
		openButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JFileChooser chooser = new JFileChooser(".");
				FileFilter ff = new ExtensionFileFilter("Images Files", 
					new String[] {"jpg", "png", "gif"});
				int status = chooser.showOpenDialog(TutorialImageViewer.this);
				if (status == JFileChooser.APPROVE_OPTION) {
					try {
						JFrame frame = new JFrame();
						JLabel label = new JLabel();
						JScrollPane sp = new JScrollPane(label);
						sp.setPreferredSize(new Dimension(560, 480));
						ImageIcon icon = new ImageIcon(chooser.getSelectedFile().toURL());
						frame.add(sp, BorderLayout.CENTER);
						String imgpath = chooser.getSelectedFile().toString();
					} catch (Exception e) {
						System.err.println("Error: " + e); 
	public static void main(String[] args) {
		TutorialImageViewer tim = new TutorialImageViewer();
class ExtensionFileFilter extends FileFilter {
 String description;
 String extensions[];
 public ExtensionFileFilter(String description, String extension) {
this(description, new String[] { extension });
 public ExtensionFileFilter(String description, String extensions[]) {
if (description == null) {
 this.description = extensions[0];
} else {
 this.description = description;
this.extensions = (String[]) extensions.clone();
 private void toLower(String array[]) {
for (int i = 0, n = array.length; i < n; i++) {
 array[i] = array[i].toLowerCase();
 public String getDescription() {
return description;
 public boolean accept(File file) {
if (file.isDirectory()) {
 return true;
} else {
 String path = file.getAbsolutePath().toLowerCase();
 for (int i = 0, n = extensions.length; i < n; i++) {
String extension = extensions[i];
if ((path.endsWith(extension) && 
(path.charAt(path.length() - extension.length() - 1)) == '.')) {
 return true;
return false;

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.