0

I have done some browsing on the internet and have found this code showing how to display multiple photos in a JFrame as seen below. What I want to be able to do is select a photo and be able to upload it to imgur through clicking a button. I know how to upload files to imgur through java but I don't know how to select one image. Anyone Have a Solution?

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ListView {

public static void main(String[] args) throws IOException {
    String path = "C:/Users/Photos";
    JFrame frame=new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();
    DefaultListModel listModel = new DefaultListModel();
    int count = 0;
    for (int i = 0; i < listOfFiles.length; i++)
    {
        System.out.println("check path"+listOfFiles[i]);
        String name = listOfFiles[i].toString();
        // load only JPEGs
        if ( name.endsWith("png") ) {
            ImageIcon ii = new ImageIcon(ImageIO.read(listOfFiles[i]));
            listModel.add(count++, ii);
        }
    }
    JList lsm=new JList(listModel);
    lsm.setVisibleRowCount(1);

    frame.add(new JScrollPane(lsm));

    frame.pack();
    frame.setVisible(true);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2434615
  • 41
  • 1
  • 3

2 Answers2

1

I don't know how to select one image.

  • JList uses an instance of ListSelectionModel to manage its selection. By default, a list selection model allows any combination of items to be selected at a time. You can specify a different selection mode(i.e, single item selection) by calling the setSelectionMode() method on the list.

     jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
  • To do, on item selection event with JList, use the ListSelectionListener to select your item.

     jList1.addListSelectionListener(new ListSelectionListener() {
    
            @Override
            public void valueChanged(ListSelectionEvent e) {
             JList jlist = (JList) e.getSource();
             Object curentSelectedObject = jlist.getModel().getElementAt(e.getFirstIndex());
             Object lastSelectedObject = jlist.getModel().getElementAt(e.getLastIndex());
            }
        });
    

Using ListSelectionListener is preferable over using MouseListener with JList. Unlike MouseListener, being simple and higher level, it has other two functions evt.getFirstIndex() and evt.getLastIndex() which is very useful.

Tutorial resource:

  1. How to Write a List Selection Listener
Sage
  • 15,290
  • 3
  • 33
  • 38
0

If you need get what was selected in your list, you can use something like this in event handler:

    ImageIcon selected = (ImageIcon)lsm.getSelectedValue();
    if(selected == null){
         // nothing selected
    }else{
         // something is selected
    }
nullptr
  • 3,320
  • 7
  • 35
  • 68