jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes To higlight the recent items getting displayed in JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "To higlight the recent items getting displayed in JList" Watch "To higlight the recent items getting displayed in JList" New topic
Author

To higlight the recent items getting displayed in JList

Nina Milo
Greenhorn

Joined: Jul 29, 2008
Posts: 19
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.


[Thumbnail for List.JPG]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Check out JList.getSelectionModel(). That returns a ListSelectionModel which has some nifty methods.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10403
    
    8

To add to Rob's tip, also check out JList#ensureIndexIsVisible(int index).

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Nina Milo
Greenhorn

Joined: Jul 29, 2008
Posts: 19
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.





[Thumbnail for Image.JPG]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Use JList.setSelectedIndex(DefaultListModel.size() - 1).
Nina Milo
Greenhorn

Joined: Jul 29, 2008
Posts: 19
Thanks Rob.
After posting the question i was working and i did the same as you said . It worked.
Nina Milo
Greenhorn

Joined: Jul 29, 2008
Posts: 19
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 .


[Thumbnail for img.JPG]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Only draw the border if the isSelected flag is true.
 
jQuery in Action, 2nd edition
 
subject: To higlight the recent items getting displayed in JList