0

I know how to display the images in separate JFrames.

public static void displayImage(BufferedImage img) {
    JFrame f = new JFrame();
    f.add(new JLabel(new ImageIcon(img)));
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

But I want a method to display all the images in a single JFrame(or in a suitable element), So I can shift between the images using two buttons("Next" and "Previous").

splungebob
  • 5,357
  • 2
  • 22
  • 45
akiaWT
  • 50
  • 1
  • 4
  • 13
  • Use a `CardLayout` ..or a single `JLabel` that displays the next/previous.. See [this answer](http://stackoverflow.com/a/9554657/418556) & scroll down to **Many Images** for more ideas. – Andrew Thompson Oct 26 '14 at 12:04

1 Answers1

1

actually you don't need a multiple frame for this.you don't need even multiple jpanels .just a single jlable is sufficient .

all you need to do is change image of jlable when button press .

1) put your images in a directory and give a name .names should have pattern

example

 C:\Users\Madhawa.se\Desktop\images\i1.jpg 
 C:\Users\Madhawa.se\Desktop\images\i2.jpg
 C:\Users\Madhawa.se\Desktop\images\i3.jpg
 C:\Users\Madhawa.se\Desktop\images\i4.jpg
 C:\Users\Madhawa.se\Desktop\images\i5.jpg

2) declare a int variable as global

int i=0;

3) increase i by one when click next button and add appropriate image to label

i++;
ImageIcon imgThisImg = new ImageIcon("C:\\Users\\Madhawa.se\\Desktop\\images\\i"+i+".jpg");

jLabel1.setIcon(imgThisImg);

4) decrease i by one when click previous button and add appropriate image to label

i--;
ImageIcon imgThisImg = new ImageIcon("C:\\Users\\Madhawa.se\\Desktop\\images\\i"+i+".jpg");

jLabel1.setIcon(imgThisImg);

you can declare also String array which holds image paths .if your image names want to be arbitrary names .and then you can get image same way.

and also you should have a logic to correctly cycle images.you should restrict i to image quantity .otherwise image doesn't exist for i after i exceeded

output>>

enter image description here

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • I found the solution I expected from here.(http://stackoverflow.com/questions/9543970/add-jlabel-with-image-to-jlist-to-show-all-the-images/9544652#9544652) – akiaWT Oct 27 '14 at 02:16