When I add a file object into a DefaultListModel, which is used to create a JList, with model.addElement(file), the displayed text in the JList is the path to the file. But I only want the file name to be displayed. I cannot do model.addElement(file.getName()), however, because I need access to the file object, not just a string, later on.
How can I add a file object to a list/model while displaying only the file name? Thank you!
You would do this by creating a custom renderer:
Then you set the renderer for the JList using:
For more information and working examples see the section from the Swing tutorial on Writing a Custom Cell Renderer