0

I'm working on UI stuff with a JList that can contain at most 8 items. Logically I have prevented the application from adding to the JList once it has reached it's limit, however, I was wondering if there was a way to perhaps explicitly set a limit on the JList itself. Which would help with the next thing I want to do, which is to repaint empty cells (up to 8 cells at most) to indicate that they are empty slots that can be filled. I'm trying to find a way to do this (perhaps by extending ListUI) which does not involve adding placeholder elements into the JList to represent empty spaces.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
endee
  • 21
  • 1
  • 4

2 Answers2

2

I think the most simple way is to implement a list model (using DefaultListModel or AbstractListModel) My idea is that the model will always have 8 items. All of them with a empty message.

If you add a new item to the model you can replace the empty text with a specific text for that item. The model will keep the track about how many items are added to the model. If you try to add more that MAX_ITEMS you can thow an exception or something like that.

Implement DefaultListModel is really easy and you have a lot of samples on Internet.

Israel
  • 446
  • 2
  • 8
2

You can try the approach shown here, which limits growth to N entries, with scrolling enabled thereafter. A ListCellRenderer, shown here, may be used to alter the item's appearance.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045