Jump to content

[Snippet] Swing Carousel Panel


Recommended Posts

Posted (edited)

Example:

71312245efc8c8f173f5f833361b138c.gif

			CarouselPanel carousel = new CarouselPanel();
			carousel.addComponent(new JLabel(new ImageIcon(ImageIO.read(new URL("http://i.imgur.com/0yd3HFb.png")))));
			carousel.addComponent(new JLabel(new ImageIcon(ImageIO.read(new URL("http://i.imgur.com/2YDrKSy.png")))));
			carousel.addComponent(new JLabel(new ImageIcon(ImageIO.read(new URL("http://i.imgur.com/e9RmCeC.png")))));
			frame.add(carousel, BorderLayout.CENTER);

Snippet:

package org.botre.swing.custom;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;

public class CarouselPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private int count = 0;
	private int index = 0;

	private JPanel content = new JPanel(new CardLayout());
	private JButton navLeft, navRight;

	private Timer timer = new Timer(1000, null);

	public CarouselPanel(int scrollseconds) {
		setLayout(new BorderLayout());
		navLeft = new JButton("<");
		navRight = new JButton(">");
		navLeft.addActionListener(l -> {
			CardLayout cl = (CardLayout)(content.getLayout());
			index = index - 1;
			index = index >= 0 ? index : count - 1;
			cl.show(content, Integer.toString(index));
			timer.restart();
		}); 
		navRight.addActionListener(l -> {
			CardLayout cl = (CardLayout)(content.getLayout());
			index = index + 1;
			index = index < count ? index : 0;
			cl.show(content, Integer.toString(index));
			timer.restart();
		}); 
		add(navLeft, BorderLayout.WEST);
		add(navRight, BorderLayout.EAST);
		add(content, BorderLayout.CENTER);
		navLeft.setVisible(count > 1);
		navRight.setVisible(count > 1);
		if(scrollseconds > 0) {
			timer = new Timer(scrollseconds * 1000, null);
			timer.addActionListener(l -> {
				navRight.doClick();
			});
			timer.setRepeats(true);
			timer.start();
		}
	}

	public CarouselPanel() {
		this(-1);
	}

	public void addComponent(Component component) {
		content.add(component, Integer.toString(count));
		count++;
		navLeft.setVisible(count > 1);
		navRight.setVisible(count > 1);
	}

}

Edited by Botre
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...