Teo Roxy
Teo Roxy

Reputation: 91

Java - How to get Next Image By Button press

I've managed to add an image into a JPanel in netbeans and display it.I wonder how to get to the next one,by pressing a button.

I've added the image using this code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent  evt){                                         
    // TODO add your handling code here:
    JFileChooser fileChooser = new JFileChooser();
    int result = fileChooser.showOpenDialog(null);
    if ( result == JFileChooser.APPROVE_OPTION ){
         String Ruta = fileChooser.getSelectedFile().getAbsolutePath();
         jTextField1.setText(Ruta);
         Icon icon = new ImageIcon(Ruta);
         jLabel2.setIcon(icon);
         JOptionPane.showMessageDialog(this,"You chose to open this file: " + 
                    fileChooser.getSelectedFile().getName());  
    } 
}

And when i press a button called "jButton2" to get the next image,without manually selecting it again from folder.

For example:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){                                         
     // TODO add your handling code here:       
} 
                      

Upvotes: 1

Views: 3338

Answers (4)

npinti
npinti

Reputation: 52185

I am assuming that you want the next image, if possible in the same directory you chose in your first code excerpt. What you could do is that once the user has chosen the image, you could use Apache's FileUtils to get the extension of files. If the file is a JPG, JPEG, PNG, etc you could load it's location in a List of strings.

This will give you a list of picture paths. You could then use the buttons to traverse the list. Once the button is pressed, you move to the next item, load the image and render it.

EDIT: This is how I would go about it step by step:

  1. Create a global variable of type List.
  2. Create a global variable which will act as a counter;
  3. In your jButton1ActionPerformed method:
    • Get the parent directory of the file that the user has chosen;
    • Use Apache's FileUtil class to get the extension of the file names. If the file name is an image, such as PNG, JPG, etc, add it (the path of that file) to your list.
  4. In you jButton2ActionPerformed, increment the counter (if the counter is not smaller than the size of your list, re-initialize it to 0, so as to avoid OutOfBoundsExceptions) and load the the file denoted by the counter using similar logic to your jButton1ActionPerformed method.

Upvotes: 0

Dieter Rehbein
Dieter Rehbein

Reputation: 991

maybe something like this:

private File allFiles;
private int currentIndex;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    JFileChooser fileChooser = new JFileChooser();
    int result = fileChooser.showOpenDialog(null);
    if ( result == JFileChooser.APPROVE_OPTION ){
            currentFile = fileChooser.getSelectedFile();
            String Ruta = currentFile.getAbsolutePath();
            jTextField1.setText(Ruta);
            allFiles = currentFile.getParent().listFiles(); // maybe you need a filter to include image files only....

            currentIndex = indexOf(allFiles, currentFile);

           Icon icon = new ImageIcon(Ruta);
                jLabel2.setIcon(icon);
                JOptionPane.showMessageDialog(this,"You chose to open this file: " + fileChooser.getSelectedFile().getName());  
    }

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    if (currentIndex+1 < allFiles.length) {
        jtextField1.setText(allFiles[++currentIndex]);
    }
}

private int indexOf(File[] files, File f) {
    for (int i=0; i+1 < files.length; i++)  {
       if (files[i].equals(f)) {
          return i;
       } 
    }
    return -1;
}

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168845

  • Get the parent file if the current image (File.getParent()).
  • Use one of the File.list..() methods to get the image files.
  • Sort them into some order that means 'next' to the user.
  • Iterate that connection until you find the current File then display the next one after that.

Upvotes: 0

Kamran Amini
Kamran Amini

Reputation: 1062

You have to enumerate images in the directory you are browsing in. When the user selects the file, you should keep a list of all images from that directory in order to retrieve them when user click the next button. As well you can get the file list whenever the user clicks the next button.

Upvotes: 2

Related Questions