Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JList selection

 
Eric Chang
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have to implement what is a highly customized File Chooser (I wish I could use the JFileChooser, but this is a bit more complex...plus JFileChooser is evil).

So in my customized chooser, I have a list of all the files and sub directories within a JList. I want the files to just be listed and grayed out and not able to be selected. I want the directories to be selectable and not grayed out. If a user double clicks on the subdirectory, it will navigate to the subdirectory and display it's contents, etc. If a user just selects the subdirectory (and not double-click), it will select that entry in the JList and fill in the JTextField elsewhere in the chooser dialog.
The part I'm stuck on is getting the JList to display selectable and unselectable items (not to mention the grayed out color of the unselectable items)...is this a case where I want to start messing around with the CellRenderer?
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Chang:
I have a list of all the files and sub directories within a JList. I want the files to just be listed and grayed out and not able to be selected. I want the directories to be selectable and not grayed out.

The part I'm stuck on is getting the JList to display selectable and unselectable items (not to mention the grayed out color of the unselectable items)...is this a case where I want to start messing around with the CellRenderer?


Yes. You want to set a cell renderer for the JList to display some items
in a greyed-out color. This is actually pretty simple. For example, to
grey-out every other item in the list:

This changes just how those items are drawn. It doesn't change whether
they can be selected or not. For that take a look at ListSelectionModel.
 
Eric Chang
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I got that part working...I'm attempting to figure out the selection part now. Thanks for your help and hopefully I can get the rest by myself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic