Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:32:51+00:00 2026-06-14T13:32:51+00:00

I have this code want to make JList talk text and icon what must

  • 0

I have this code want to make JList talk text and icon what must to do.
Some persons advice me to make list of label and in table put text an icon is it Possible? How?

Search on ///////////////// for embedded comment.

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileSystemView;

public class pan extends JPanel implements DropTargetListener {

private DefaultListModel listModel = new DefaultListModel();
private DropTarget dropTarget;
private JLabel jLabel1;
private JScrollPane jScrollPane1;
private JList list;

/**
 * Create the panel.
 */
public pan() {
    setLayout(null);        
    list = new JList();
    dropTarget = new DropTarget(list, this);
    list.setModel(listModel);
    list.setDragEnabled(true);
    //list.setTransferHandler(new FileTransferHandler());
    jScrollPane1 = new JScrollPane();

    jScrollPane1.setViewportView(list);
    jScrollPane1.setBounds(10, 150, 635, 330);
    add(jScrollPane1);

}
public void dragEnter(DropTargetDragEvent arg0) {
    // nothing
}

public void dragOver(DropTargetDragEvent arg0) {
    // nothing
}

public void dropActionChanged(DropTargetDragEvent arg0) {
    // nothing
}

public void dragExit(DropTargetEvent arg0) {
    // nothing
}

public void drop(DropTargetDropEvent evt) {
    int action = evt.getDropAction();
    evt.acceptDrop(action);
    try {
        Transferable data = evt.getTransferable();
        if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
              List<File> files = (List<File>) data.getTransferDat(DataFlavor.javaFileListFlavor);
            for (File file : files) {

    Icon icon=FileSystemView.getFileSystemView().getSystemIcon(file);
/////////////////listModel.addElement(filepath+icon);///////////////////////want code do this

            }
        }
    } catch (UnsupportedFlavorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        evt.dropComplete(true);
    }
 }

}

After solve this problem i will add action to each element if doubel click on it will open desktop
to path stored in list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:32:52+00:00Added an answer on June 14, 2026 at 1:32 pm

    FileDragList

    import java.awt.*;
    import java.awt.datatransfer.*;
    import java.awt.dnd.*;
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    import javax.swing.filechooser.FileSystemView;
    import java.io.*;
    import java.util.List;
    
    public class FileDropList extends JPanel implements DropTargetListener {
    
        private DefaultListModel listModel = new DefaultListModel();
        private DropTarget dropTarget;
        private JLabel jLabel1;
        private JScrollPane jScrollPane1;
        private JList list;
    
        /**
         * Create the panel.
         */
        public FileDropList() {
            //setLayout(null);        
            list = new JList();
            dropTarget = new DropTarget(list, this);
            list.setModel(listModel);
            list.setDragEnabled(true);
            FileListCellRenderer renderer = new FileListCellRenderer();
            list.setCellRenderer(renderer);
            //list.setTransferHandler(new FileTransferHandler());
            jScrollPane1 = new JScrollPane(list);
    
            //jScrollPane1.setBounds(10, 150, 635, 330);
            add(jScrollPane1);
    
        }
        public static void main(String[] args) throws Exception {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    JPanel pan = new FileDropList();
                    pan.setBorder(new LineBorder(Color.BLACK));
                    JOptionPane.showMessageDialog(null, pan);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    
        public void dragEnter(DropTargetDragEvent arg0) {
            // nothing
        }
    
        public void dragOver(DropTargetDragEvent arg0) {
            // nothing
        }
    
        public void dropActionChanged(DropTargetDragEvent arg0) {
            // nothing
        }
    
        public void dragExit(DropTargetEvent arg0) {
            // nothing
        }
    
        public void drop(DropTargetDropEvent evt) {
            int action = evt.getDropAction();
            evt.acceptDrop(action);
            try {
                Transferable data = evt.getTransferable();
                if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    List<File> files = (List<File>) data.getTransferData(DataFlavor.javaFileListFlavor);
                    for (File file : files) {
                        listModel.addElement(file);
                    }
                }
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                evt.dropComplete(true);
            }
        }
    }
    
    /** A FileListCellRenderer for a File. */
    class FileListCellRenderer extends DefaultListCellRenderer {
    
        private static final long serialVersionUID = -7799441088157759804L;
        private FileSystemView fileSystemView;
        private JLabel label;
        private Color textSelectionColor = Color.BLACK;
        private Color backgroundSelectionColor = Color.CYAN;
        private Color textNonSelectionColor = Color.BLACK;
        private Color backgroundNonSelectionColor = Color.WHITE;
    
        FileListCellRenderer() {
            label = new JLabel();
            label.setOpaque(true);
            fileSystemView = FileSystemView.getFileSystemView();
        }
    
        @Override
        public Component getListCellRendererComponent(
                JList list,
                Object value,
                int index,
                boolean selected,
                boolean expanded) {
    
            File file = (File)value;
            label.setIcon(fileSystemView.getSystemIcon(file));
            label.setText(fileSystemView.getSystemDisplayName(file));
            label.setToolTipText(file.getPath());
    
            if (selected) {
                label.setBackground(backgroundSelectionColor);
                label.setForeground(textSelectionColor);
            } else {
                label.setBackground(backgroundNonSelectionColor);
                label.setForeground(textNonSelectionColor);
            }
    
            return label;
        }
    }
    

    Original Answer

    import java.awt.datatransfer.*;
    import java.awt.dnd.*;
    import javax.swing.*;
    import javax.swing.filechooser.FileSystemView;
    import java.io.*;
    import java.util.List;
    
    public class FileDragList extends JPanel implements DropTargetListener {
    
        private static final long serialVersionUID = 1L;
        private DefaultListModel listModel = new DefaultListModel();
        private JScrollPane jScrollPane1;
        private JList list;
    
        /**
         * Create the panel.
         */
        public FileDragList() {
            setLayout(null);        
            list = new JList();
            DropTarget dropTarget = new DropTarget(list, this);
            list.setModel(listModel);
            list.setDragEnabled(true);
            FileListCellRenderer renderer = new FileListCellRenderer();
            list.setCellRenderer(renderer);
            //list.setTransferHandler(new FileTransferHandler());
            jScrollPane1 = new JScrollPane();
    
            jScrollPane1.setViewportView(list);
            jScrollPane1.setBounds(10, 150, 635, 330);
            add(jScrollPane1);
    
        }
        public static void main(String[] args) throws Exception {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    JOptionPane.showMessageDialog(null, new FileDragList());
                }
            };
            SwingUtilities.invokeLater(r);
        }
    
        public void dragEnter(DropTargetDragEvent arg0) {
            // nothing
        }
    
        public void dragOver(DropTargetDragEvent arg0) {
            // nothing
        }
    
        public void dropActionChanged(DropTargetDragEvent arg0) {
            // nothing
        }
    
        public void dragExit(DropTargetEvent arg0) {
            // nothing
        }
    
        public void drop(DropTargetDropEvent evt) {
            int action = evt.getDropAction();
            evt.acceptDrop(action);
            try {
                Transferable data = evt.getTransferable();
                if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    List<File> files = (List<File>) data.getTransferData(DataFlavor.javaFileListFlavor);
                    for (File file : files) {
    
                        Icon icon=FileSystemView.getFileSystemView().getSystemIcon(file);
                        /////////////////listModel.addElement(filepath+icon);///////////////////////want code do this
    
                    }
                }
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                evt.dropComplete(true);
            }
        }
    }
    

    Icon icon=FileSystemView.getFileSystemView().getSystemIcon(file);
    

    Oh right.. Try a variant of the FileTreeCellRenderer of File Browser GUI.

    That’s it, used for the tree on the left.

    FileTreeCellRenderer

    /** A TreeCellRenderer for a File. */
    class FileTreeCellRenderer extends DefaultTreeCellRenderer {
    
        private FileSystemView fileSystemView;
        private JLabel label;
    
        FileTreeCellRenderer() {
            label = new JLabel();
            label.setOpaque(true);
            fileSystemView = FileSystemView.getFileSystemView();
        }
    
        @Override
        public Component getTreeCellRendererComponent(
            JTree tree,
            Object value,
            boolean selected,
            boolean expanded,
            boolean leaf,
            int row,
            boolean hasFocus) {
    
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
            File file = (File)node.getUserObject();
            label.setIcon(fileSystemView.getSystemIcon(file));
            label.setText(fileSystemView.getSystemDisplayName(file));
            label.setToolTipText(file.getPath());
    
            if (selected) {
                label.setBackground(backgroundSelectionColor);
                label.setForeground(textSelectionColor);
            } else {
                label.setBackground(backgroundNonSelectionColor);
                label.setForeground(textNonSelectionColor);
            }
    
            return label;
        }
    }
    

    Update

    An SSCCE that adapts the renderer above for lists.

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.filechooser.FileSystemView;
    import java.io.File;
    
    public class FileList {
        public static void main(String[] args) throws Exception {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    JPanel gui = new JPanel(new BorderLayout(2,2));
    
                    File userHome = new File(System.getProperty("user.home"));
                    File[] files = userHome.listFiles();
                    JList list = new JList(files);
                    list.setCellRenderer(new FileListCellRenderer());
                    gui.add(new JScrollPane(list));
    
                    JOptionPane.showMessageDialog(null, gui);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }
    
    /** A FileListCellRenderer for a File. */
    class FileListCellRenderer extends DefaultListCellRenderer {
    
        private static final long serialVersionUID = -7799441088157759804L;
        private FileSystemView fileSystemView;
        private JLabel label;
        private Color textSelectionColor = Color.BLACK;
        private Color backgroundSelectionColor = Color.CYAN;
        private Color textNonSelectionColor = Color.BLACK;
        private Color backgroundNonSelectionColor = Color.WHITE;
    
        FileListCellRenderer() {
            label = new JLabel();
            label.setOpaque(true);
            fileSystemView = FileSystemView.getFileSystemView();
        }
    
        @Override
        public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean selected,
            boolean expanded) {
    
            File file = (File)value;
            label.setIcon(fileSystemView.getSystemIcon(file));
            label.setText(fileSystemView.getSystemDisplayName(file));
            label.setToolTipText(file.getPath());
    
            if (selected) {
                label.setBackground(backgroundSelectionColor);
                label.setForeground(textSelectionColor);
            } else {
                label.setBackground(backgroundNonSelectionColor);
                label.setForeground(textNonSelectionColor);
            }
    
            return label;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that I want to make point-free; (\k t -> chr
So i have this code which makes a box, but want to make the
I have this code which is ok $row->main_auto_make_make But I want to make something
I have this code, this works perfect. Only i want to make this dynamic
I have this code: $node = new Node(); Now I want to make the
I have this code to make a Jbutton with icon image ,and it works.But
I have this code, and works perfectly, but i want to make a simple
I have this HTML field: <input type=text name=userInput id=userInput> I want to make sure
I have this code and I want to have my button as a square,
Hi can you help me with this?? I have this code and i want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.