Please have a look on the following code. Try to run it. Check the code and let me know do we have any other good way to achieve the same thing?

Code:


Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
 
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
 
public class SamplesList {
	
	static int previndex = -1;
	static JList jlist2 = null;
  public static void main(String args[]) {
    JFrame frame = new JFrame("Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DefaultListModel model = new DefaultListModel();
    
    
    
    
    model.ensureCapacity(100);
    for (int i = 0; i < 100; i++) {
      model.addElement(Integer.toString(i));
    }
     jlist2 = new JList(model);
    
    
    jlist2.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jList1MouseClicked(evt);
            //repaint();
        }
        public void mouseExited(java.awt.event.MouseEvent evt) {
            jList1MouseExited(evt);
            //repaint();
        }
    });
    jlist2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
        public void mouseMoved(java.awt.event.MouseEvent evt) {
            jList1MouseMoved(evt);
            //repaint();
        }
    });
 
    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);
 
    frame.setSize(300, 350);
    frame.setVisible(true);
 
    jlist2.ensureIndexIsVisible(50);
    
    jlist2.removeSelectionInterval(previndex,previndex);
  }
  
  private static void jList1MouseMoved(java.awt.event.MouseEvent evt) {
  	
    Object o = evt.getSource();
    javax.swing.JList oj=(javax.swing.JList)o;
    int indexis = oj.locationToIndex( evt.getPoint());
    System.out.println("Hello ::::::"+previndex+"::::"+indexis);
    if(previndex!=indexis){
        oj.setSelectedIndex(indexis);
        oj.setSelectionBackground(java.awt.Color.BLUE);
    }else{
    	oj.setSelectedIndex(previndex);
        oj.setSelectionBackground(java.awt.Color.GREEN);
    	
    }
 
}
 
private static void jList1MouseClicked(java.awt.event.MouseEvent evt) {
    
    Object o = evt.getSource();
    javax.swing.JList oj=(javax.swing.JList)o;
    int indexis = oj.locationToIndex( evt.getPoint());
    previndex = indexis;
    oj.setSelectedIndex(indexis);
    oj.setSelectionBackground(java.awt.Color.GREEN);
    oj.repaint();
    //oj.removeSelectionInterval(previndex,previndex);
    
    changeModel();
    
}
 
private static void jList1MouseExited(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    Object o = evt.getSource();
    javax.swing.JList oj=(javax.swing.JList)o;
    if(previndex!=-1){
    oj.setSelectedIndex(previndex);
    oj.setSelectionBackground(java.awt.Color.GREEN);
    }
}//GEN-LAST:event_jList1MouseExited
 
private static void changeModel(){
	
	
	DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(100);
    for (int i = 0; i < 100; i++) {
      
      if(i==previndex){
      	model.addElement("<html><body bgcolor=\"#00FF00\" width=\"300\" height=\"20\" "+Integer.toString(i)+"</body></html>");
      }else{
      	
      	model.addElement(Integer.toString(i));
      }
    }
	jlist2.setModel(model);
	
}
 
}
Regards,
Darshak.