I have made two JScrollPane components. The left one shows the directories in USB and right shows the directories of a local drive. For I am able to display the complete file path as shown in first pic. But I want to display it with icons just like the way directories are displayed in any local drive on PC. This is my designed GUI, here is the example of what I want example

public class MainForm extends javax.swing.JFrame { iRecordCopy obj = new iRecordCopy();

public MainForm() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jScrollPane2 = new javax.swing.JScrollPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Main From");
    setLocation(new java.awt.Point(0, 0));
    setName("mainframe"); // NOI18N

    jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
    jLabel1.setText("iRecordCopy");

    jLabel2.setText("jLabel2");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel2)
            .addGap(48, 48, 48)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap(25, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2))
            .addGap(30, 30, 30))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE))
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
                .addComponent(jScrollPane2))
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
      
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MainForm().setVisible(true);
        }
    });
}

public void showusb()
{
    File[] usb;
    int count = 0;
    String str;
    usb = obj.detectUSB();
          
    if(usb == null)
    {
        //USB_window.setText("No USB attached\n");
        return;
    }
    
    for (File filename : usb) {
        str = filename.toString().substring(3);
        str = str.substring(0, str.length() - 3);
        
        System.out.println(str);
        
        if(str.equalsIgnoreCase("vehicle"))
        { 
            System.out.print(filename.toString());   
            //USB_window.append(filename.getPath()+"\n");
            count++;
        }
    }
    if(count == 0)
    {
         System.out.println("No recordings found in usb");
    }
    else
        System.out.println("Number of directries :" + count);
    
    
    
    //File currentDir = new File(System.getProperty("user.home"));
    JList<File> jlist=new JList<File>(obj.path.listFiles());
    jScrollPane1 = new JScrollPane(jlist);
    jScrollPane1.setPreferredSize(new Dimension(400, 800));
    
    setContentPane(jScrollPane1);
    
    final JLabel label=new JLabel();
    jlist.setCellRenderer(new ListCellRenderer<File>() {
        @Override
        public Component getListCellRendererComponent(JList<? extends File> list, File value, int index,
                boolean isSelected, boolean cellHasFocus) {
            label.setIcon(FileSystemView.getFileSystemView().getSystemIcon(value));
            label.setText(value.getName());
            return label;
        }
    });   
    
}

public void showPc(File f)
{
    JList<File> jlist=new JList<File>(f.listFiles());
    jScrollPane2 = new JScrollPane(jlist);
    //jScrollPane2.setPreferredSize(new Dimension(400, 800));
    
    setContentPane(jScrollPane2);
    
    final JLabel label=new JLabel();
    jlist.setCellRenderer(new ListCellRenderer<File>() {
        @Override
        public Component getListCellRendererComponent(JList<? extends File> list, File value, int index,
                boolean isSelected, boolean cellHasFocus) {
            label.setIcon(FileSystemView.getFileSystemView().getSystemIcon(value));
            label.setText(value.getName());
            return label;
        }
    });   
    
}

// Variables declaration - do not modify                     
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
// End of variables declaration                   

}

1

There are 1 answers

3
Rocco On BEST ANSWER

Here a short example to create a minimal JList with file icons, it displays the content of the user's home directory using the system file icons.

package test;

import java.awt.Component;
import java.awt.Dimension;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.filechooser.FileSystemView;

public class Test {
    public static void main(String[] args) {
        
        
        JFrame frame=new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        File currentDir=new File(System.getProperty("user.home"));
        JList<File> jlist=new JList<File>(currentDir.listFiles());
        JScrollPane scrollPane=new JScrollPane(jlist);
        scrollPane.setPreferredSize(new Dimension(400, 800));
        
        
        frame.setContentPane(scrollPane);
        
        final JLabel label=new JLabel();
        jlist.setCellRenderer(new ListCellRenderer<File>() {
            @Override
            public Component getListCellRendererComponent(JList<? extends File> list, File value, int index,
                    boolean isSelected, boolean cellHasFocus) {
                label.setIcon(FileSystemView.getFileSystemView().getSystemIcon(value));
                label.setText(value.getName());
                return label;
            }
        });
        
        frame.pack();
        frame.setVisible(true);
        
        
    }
}