Create an image slider in Java Swing

In this tutorial, we will learn how to create an image slider in Java using JFrame and Swing. Here we create a simple image slider with buttons that allows us to move forward and backward that allows us to slide through images.

JFrame class is the most common Swing container, it provides a window with borders, title, minimize, maximize and close buttons on the title bar.

Simple Image Slider Java Program using Swing

  • Import  AWT package.
  • Import Swing package.
  • Implement ActionListener.
import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 class Imageslider extends JFrame  implements ActionListener
{

ImageIcon s[];
JLabel l;
JButton b1,b2;
int i,l1;
JPanel p;

public Imageslider()
{
    setLayout(new BorderLayout( ));
    setSize(800, 700);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    JPanel p=new JPanel(new FlowLayout());
    b1=new JButton("<<");
    b2=new JButton(">>");
    p.add(b1);
    p.add(b2);
    add(p,BorderLayout.SOUTH);
    b1.addActionListener(this);
    b2.addActionListener(this);
    s = new ImageIcon[2]; 
    s[0] = new ImageIcon("c:/slide2.jpg");
    s[1] = new ImageIcon("c:/slide3.jpg");
    l = new JLabel("",JLabel.CENTER); 
    add(l,BorderLayout.CENTER);
    l.setIcon(s[0]);
    
}
public  void actionPerformed(ActionEvent e)
{

    if(e.getSource()==b1)
    {
        if(i==0)
        {
            JOptionPane.showMessageDialog(null,"This is First Image");
        }
        else
            {
            i=i-1;
            l.setIcon(s[i]);
        }
    }
    if(e.getSource()==b2)
    {
        if(i==s.length-1)
        {
            JOptionPane.showMessageDialog(null,"This is LastImage");
        }
        else
            {
            i=i+1;
            l.setIcon(s[i]);
            }
        }

     }
public static void main(String args[])
{
    Imageslider obj = new Imageslider();
}
 }

JButton is also known as a push button. The user presses or clicks a JButton to perform an action.

Upon clicking the button the required action is performed. The Java ActionListener is notified whenever you click on the button. It is notified against ActionEvent.

We can create a JButton with its text as ‘PREV’ in the following code.

JButton b2 = new JButton("prev");

We would like to handle the button click event, so we add an action listener to the button b2 as below.

b2.addActionListener(this);

ImageIcon has several constructors, including:

ImageIcon(URL location)

It is the constructor that is used in the above program that creates an ImageIcon from the specified URL.

Output:

The output of our Java swing image slider will look like you can see below in the picture:

image slider in Java Swing

More Tutorials:

How to draw various shapes in Java Swing?

How to draw a wave form pattern in java swing?

 

2 responses to “Create an image slider in Java Swing”

  1. Mandar Patel says:

    yo, it really didn’t help me with anything! Thanks for nothing

  2. ali says:

    Thank you. İ finally found it what i want. its very helpful

Leave a Reply