0

In this program, I am trying to select images from users using filechooser and then displaying those images. By default I added 2 images. When I add more images it doesn't display?

Below is my entire code

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.FlowLayout;
import java.io.File;
import javax.swing.JDialog;
import javax.swing.JFileChooser;

public class CLayout {
    JFrame frame = new JFrame("CardLayout demo");
    JPanel panelCont = new JPanel();
    LoginView log = new LoginView();
    JPanel Img = new ImageGallery();
    CardLayout cl = new CardLayout();

    public CLayout() {       
        panelCont.setLayout(cl);
        log.setLayout(new BoxLayout(log, BoxLayout.PAGE_AXIS));
        Img.setLayout(new BoxLayout(Img, BoxLayout.PAGE_AXIS)); 
        panelCont.add(log, "1");
        panelCont.add(Img, "2");
        cl.show(panelCont, "1");
        ActionListener loginListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String userName = log.getUserName();
                char[] password = log.getPassword();
                String pass=new String(password);
                if (LoginView.LOGIN.equals(e.getActionCommand())) {
                    if(userName.equals("imagegallery")&& pass.equals("12345"))
                         cl.show(panelCont, "2");
                }
           }
       };
       log.addActionListener(loginListener);
       frame.add(panelCont);
       frame.setSize(800,600);
       frame.setTitle("     Image Gallery    ");
       frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       frame.pack();
       frame.setVisible(true);  
  }

  public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
              new CLayout();
          }
      });
  }

}
class ImageGallery extends JPanel {
    private ImageIcon myImage1 = new ImageIcon ("Chrysanthemum.jpg");
    private ImageIcon myImage2 = new ImageIcon ("Desert.jpg");

    JPanel ImageGallery = new JPanel();
    private ImageIcon[] myImages =new ImageIcon[10];
    private int curImageIndex=0;
    private int count=0;
    private int total=1;
    public ImageGallery () {   
        ImageGallery.add(new JLabel (myImage1));
        myImages[0]=myImage1;
        myImages[1]=myImage2;
        add(ImageGallery, BorderLayout.CENTER);

        JButton PREVIOUS = new JButton ("Previous");
        JButton NEXT = new JButton ("Next");
        JDialog.setDefaultLookAndFeelDecorated(true);

        JButton button = new JButton("Select File");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    System.out.println(selectedFile.getName().toString());
                    ImageIcon tImage=new ImageIcon("selectedFile.getName().toString()");
                    System.out.println(myImages.length);
                    int n=2+count;
                    myImages[n]=tImage;
                    total=total+count+1;
                    count++;
                    System.out.println(total+"  "+count);
                }
            }
        });
        JPanel Menu = new JPanel();
        Menu.setLayout(new GridLayout(1,3));
        add(Menu, BorderLayout.NORTH);
        Menu.add(PREVIOUS);
        Menu.add(NEXT);
        Menu.add(button);

        //register listener
        PreviousButtonListener PreviousButton = new PreviousButtonListener ();            
        NextButtonListener NextButton = new NextButtonListener ();

        //add listeners to corresponding componenets 
        PREVIOUS.addActionListener(PreviousButton);
        NEXT.addActionListener(NextButton);
    }


class PreviousButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        if(curImageIndex>0 && curImageIndex <=total) {
            ImageGallery.remove(0);
            curImageIndex=curImageIndex-1;
            ImageIcon TheImage= myImages[curImageIndex];
            ImageGallery.add(new JLabel (TheImage));
            ImageGallery.validate();
            ImageGallery.repaint(); 
        } else {   
            ImageGallery.remove(0);
            ImageGallery.add(new JLabel (myImage1));
            curImageIndex=0;
            ImageGallery.validate();
            ImageGallery.repaint();
        }
    }
}


class NextButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        if(curImageIndex>=0 && curImageIndex <total){   
            ImageGallery.remove(0);
            curImageIndex = curImageIndex + 1;
            ImageIcon TheImage= myImages[curImageIndex];
            ImageGallery.add(new JLabel (TheImage));
            ImageGallery.validate();
            ImageGallery.repaint(); 
        } else {   
            ImageGallery.remove(0);
            ImageGallery.add(new JLabel (myImages[total]));
            curImageIndex=total ;
            ImageGallery.validate();
            ImageGallery.repaint();
        }
    }
}
}
class LoginView extends JPanel {

    JLabel userLabel = new JLabel("User");
    JTextField userText = new JTextField(20);
    JLabel passwordLabel = new JLabel("Password");
    JPasswordField passwordText = new JPasswordField(20);
    private final JButton loginButton;
    private final JButton registerButton;

    public static final String LOGIN = "Login";
    public static final String REGISTER = "Regster";

    public LoginView() {
        setLayout(new GridLayout(0, 2));
        userLabel.setBounds(10, 10, 80, 25);
        add(userLabel);
        userText.setBounds(10, 10, 60, 25);
        add(userText);
        passwordLabel.setBounds(10, 40, 80, 25);
        add(passwordLabel);
        passwordText.setBounds(100, 40, 160, 25);
        add(passwordText);

        loginButton = new JButton("login");
        loginButton.setActionCommand(LOGIN);
        registerButton = new JButton("register");
        registerButton.setActionCommand(REGISTER);
        add(loginButton);
    }

    public void addActionListener(ActionListener listener) {
        loginButton.addActionListener(listener);
        registerButton.addActionListener(listener);
    }

    public String getUserName() {
        return userText.getText();
    }

    public char[] getPassword() {
        return passwordText.getPassword();
    }
}
1
  • 2
    did you hear about sscce ? Commented Jul 10, 2014 at 8:58

1 Answer 1

3

Problem is in this line

ImageIcon tImage=new ImageIcon("selectedFile.getName().toString()");

you need to remove double-quotes in order to read actual file name.

ImageIcon tImage=new ImageIcon(selectedFile.getName().toString());

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I think selectedFile.getAbsolutePath() is more useful. I suspect selectedFile.getName() will return the file name only not full path

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.