TAGS :Viewed: 26 - Published at: a few seconds ago

[ how to work on JList ]

I want to add files in JList and when user selects the file and clicks open then he should be able to open that selected.

I am trying to code this, will I be able to only display the path of the file in the list? So that when user clicks then we get that path where user clicked and open the file.

If so then, is it possible to list only the filename and not its paths so that even then when user clicks on that file name he should be able to open it.

If its possible just tell me how to do it na.. I don't want coding i just need a practical idea on this..i am coding all this using java..

Answer 1


Get a list of File objects. Place these in some kind of ListModel, DefaultListModel should be capable of supporting this functionality.

Design a ListCellRenderer which is capable of returning the visual representation of what you want displayed. I'd use something like DefaultListCellRenderer, which is based on a JLabel, but has inbuilt support for selection formatting, you would simply need to call setText after you've used the super implementation.

You can use a ListSelectionListener to monitor changes to the list's selection and disable/enable the open button.

When the user clicks the open button, you can get the currently selected item from the JList, which will return a File reference (from you model).

You can then use java.awt.Desktop to open it

Take a closer look at How to use Lists and How to intergrate with the Desktop class for more details