Using JLists and ListModels

Posted by devoured elysium on Stack Overflow See other posts from Stack Overflow or by devoured elysium
Published on 2010-05-12T23:48:14Z Indexed on 2010/05/12 23:54 UTC
Read the original article Hit count: 164

Filed under:
|
|

I have defined a DirectoryListModel class that extends the AbstractListModel class from the Java Api.

Internally, I have a list of File objects. I have defined the getElementAt(int index) method as:

@Override
public Object getElementAt(int index) {
    return directoryElements.get(index)
}

The problem is that when I try to run my JList with my DirectoryListModel, it is going to show up the full paths of files instead of just the filenames. I could change this code to:

@Override
public Object getElementAt(int index) {
    return directoryElements.get(index).getName();

}

and it'd work wonders, but the problem is that in the onclick event I'll want to have the File objects so I can do some checking with them (check if they're directories, etc). If I make getElementAt() return a String, I'm losing that possibility, thus I'd like to konw if there is a way I can format my File objects before the JList shows them in my window.

Thanks

© Stack Overflow or respective owner

Related posts about jlist

Related posts about java