Question

Hi i am trying to use slide image using prev and next button button i am not able to this i did following code to achieve my output but when i run my program and try to slide image it shows image not available i. i am not getting why it is happening. i found this code from a website

   public class ImageSlider extends JPanel implements ActionListener {

    private static final int MAX = 20;
    private static final Font sans = new Font("SansSerif", Font.PLAIN, 16);
    private static final Border border =
        BorderFactory.createMatteBorder(4, 16, 4, 16, Color.lightGray);
    private List<String> list = new ArrayList<String>(MAX);
    private List<ImageIcon> cache = new ArrayList<ImageIcon>(MAX);
    private JLabel imageLabel = new JLabel();
    private JButton prevButton = new JButton();
    private JButton nextButton = new JButton();
    private JComboBox favorites;

    public ImageSlider() {
        this.setLayout(new BorderLayout());

        list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
        list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\a0.png");
        list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
         list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\a0.png");

        for (int i = 0; i < list.size(); i++) cache.add(i, null);

        JLabel titleLabel = new JLabel();
        titleLabel.setText("ImageSlider");
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
        titleLabel.setBorder(border);
        this.add(titleLabel, BorderLayout.NORTH);

        imageLabel.setIcon(getImage(0));
        imageLabel.setHorizontalAlignment(JLabel.CENTER);
        imageLabel.setBorder(border);
        this.add(imageLabel, BorderLayout.CENTER);

        favorites = new JComboBox(
            list.toArray(new String[list.size()]));
        favorites.setActionCommand("favs");
        favorites.addActionListener(this);

        prevButton.setText("\u22b2Prev");
        prevButton.setFont(sans);
        prevButton.setActionCommand("prev");
        prevButton.addActionListener(this);

        nextButton.setText("Next\u22b3");
        nextButton.setFont(sans);
        nextButton.setActionCommand("next");
        nextButton.addActionListener(this);

        JPanel controlPanel = new JPanel();
        controlPanel.add(prevButton);
        controlPanel.add(favorites);
        controlPanel.add(nextButton);
        controlPanel.setBorder(border);
        this.add(controlPanel, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent ae) {
        String cmd = ae.getActionCommand();
        if ("favs".equals(cmd)) {
            int index = favorites.getSelectedIndex();
            ImageIcon image = getImage(index);
            imageLabel.setIcon(image);
            if (image != null) imageLabel.setText("");
            else imageLabel.setText("Image not available.");
        }
        if ("prev".equals(cmd)) {
            int index = favorites.getSelectedIndex() - 1;
            if (index < 0) index = list.size() - 1;
            favorites.setSelectedIndex(index);
        }
        if ("next".equals(cmd)) {
            int index = favorites.getSelectedIndex() + 1;
            if (index > list.size() - 1) index = 0;
            favorites.setSelectedIndex(index);
        }
    }

    public JButton getDefault() { return nextButton; }

    // Return the (possibly cached) image having the given index.
    private ImageIcon getImage(int index) {
        ImageIcon image = cache.get(index);
        if (image != null) return image;
        String name = "images/" + list.get(index);
        URL url = ImageSlider.class.getResource(name);
        if (url != null) {
            image = new ImageIcon(url);
        }
        cache.set(index, image);
        return image;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                ImageSlider go = new ImageSlider();
                frame.add(go);
                frame.setTitle("ImageSlider");
                frame.setSize(400, 300);
                frame.setVisible(true);
                go.getDefault().requestFocusInWindow();
            }
        });
    }
}

please tell me where i am wrong Thanks in advance

Was it helpful?

Solution

" i found this code from a website"

Copying ans pasting code is never the way to go. Learn how/why it works, and create your own code that uses the found code as a reference.

" try to slide image it shows image not available i."

Without running your program, because I don't have the image resources, I could see this problem. Your code, uses a getResource(), which will look for images in the class path.

URL url = ImageSlider.class.getResource(name);

The path name That is passed to it requires a class path reference. But what you are doing is passing an absolute path

list.add("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png");
...
String name = "images/" + list.get(index);

The way this code is set up, it looks like the producer of the code had their images folder inside the src and just passed/added the image file name e.g. "image.png"

list.add("image.png");

You your file structure should look like

ProjectRoot
         src
             images
                  image.png

NOTE: This is all just an educated guess. Try it out (change the image location and the paths. Also if it doesn't work, try to add a / before "images/""/images/"

OTHER TIPS

Try This Code: This is simple code to slide images.

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



 class myImage extends JFrame  implements ActionListener
{

ImageIcon m[];
JLabel l;
JButton b,b1;
int i,l1;
JPanel p;

public myImage()
{
    setLayout(new BorderLayout( ));
    setSize(1000, 1000);
    setVisible(true);
    JPanel p=new JPanel(new FlowLayout());
    b=new JButton("Pre");
    b1=new JButton("Next");
    p.add(b);
    p.add(b1);
    add(p,BorderLayout.SOUTH);
    b.addActionListener(this);
    b1.addActionListener(this);
    m = new ImageIcon[2];
    m[0] = new ImageIcon("m.jpg");
    m[1] = new ImageIcon("m1.jpg");
    l = new JLabel();
    l.setBounds(400, 0, getWidth(), getHeight());
    add(l,BorderLayout.CENTER);
    l.setIcon(m[0]);

}
public  void actionPerformed(ActionEvent e)
{

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

     }


public static void main(String args[])
{
    myImage i1 = new myImage();
}


 }

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top