• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

To higlight the recent items getting displayed in JList

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please open the attachment before reading below.

I have displayed 4 JList's in a row , where each of the Lists has barcode getting
displayed from the input read through machine. Input read sequence is
For 1st piece read - List1,List2, List3 and List4 items are displayed
For 2st piece read - List1,List2, List3 and List4 items are displayed
For 3rd piece read - List1,List2, List3 and List4 items are displayed
:
:
Repeats for N pieces.

My question is how to automatically select or highlight the recent last item
getting displayed in 4 lists without user selecting the item. This is required for
the user to view and find which was the last piece read by the machine.

Any help is accepted and thanks for consideration.
List.JPG
[Thumbnail for List.JPG]
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out JList.getSelectionModel(). That returns a ListSelectionModel which has some nifty methods.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to Rob's tip, also check out JList#ensureIndexIsVisible(int index).
 
Nina Milo
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answers.
I have solved the problem myself by doing :

This will automatically select the last recent item displayed in 4 Lists on the frame. But still i have one issue with on of the List's on the frame.

Please look at the attached file. Current working behavior is shown in Frame1 . The expected behavior is shown in Frame2.

In Frame1 :
The recent items are selected in List1 , List2, List3 and List4 as soon as the machine reads. But the last item in List4 is not selected as same number 10 is repeating several times. It selects the first occurrence and
i assume because DefaultListModel closely behaves as vector and the lastElement() returned for same numbers in the vector will be first occurrence. I have to make it work as in Frame2. Please suggest any ideas.

Thanks.


Image.JPG
[Thumbnail for Image.JPG]
Image
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use JList.setSelectedIndex(DefaultListModel.size() - 1).
 
Nina Milo
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob.
After posting the question i was working and i did the same as you said . It worked.
 
Nina Milo
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an extended question to this post.
How could i draw a line border around the selected item instead of highlighting? I have implemented ListCellRenderer and have set the border with LineBorder but that does for each item and not only the last item in the list.
I need to have a border because some of the text in the Lists can have different colors and it gets hidden if selected by index as by default it sets the List background and foreground. It should look like the one i have in the attached file.

Thanks .
img.JPG
[Thumbnail for img.JPG]
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only draw the border if the isSelected flag is true.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic