Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.catexpress.gui.tools; import com.alee.laf.text.WebTextField; import com.catexpress.model.Cliente; import java.awt.Component; import javax.swing.BorderFactory; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; import org.apache.commons.lang3.StringUtils; /** * * @author Gustavo */ public class AutoCompletarCellRenderer extends JLabel implements ListCellRenderer<Cliente> { private final static String MATCH_COLOR = "#d63838"; private final WebTextField regex; public AutoCompletarCellRenderer(WebTextField regex) { this.regex = regex; } @Override public Component getListCellRendererComponent(JList list, Cliente cliente, int index, boolean isSelected, boolean cellHasFocus) { setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); if (cliente == null) { setText("Ningun resultado encontrado"); return this; } String replace = "<html>"; if (StringUtils.containsIgnoreCase(String.valueOf(cliente.getId()), regex.getText())) { replace += String.valueOf(cliente.getId()).replaceAll("(?i)" + regex.getText(), "<font color='" + MATCH_COLOR + "'>" + regex.getText() + "</font>"); } else { replace += cliente.getId(); } if (StringUtils.containsIgnoreCase(cliente.getNombre(), regex.getText())) { replace += " - " + cliente.getNombre().replaceAll("(?i)" + regex.getText(), "<font color='" + MATCH_COLOR + "'>" + regex.getText() + "</font>"); } else { replace += " - " + cliente.getNombre(); } if (StringUtils.containsIgnoreCase(cliente.getApPaterno(), regex.getText())) { replace += " " + cliente.getApPaterno().replaceAll("(?i)" + regex.getText(), "<font color='" + MATCH_COLOR + "'>" + regex.getText() + "</font>"); } else { replace += " " + cliente.getApPaterno(); } if (StringUtils.containsIgnoreCase(cliente.getApMaterno(), regex.getText())) { replace += " " + cliente.getApMaterno().replaceAll("(?i)" + regex.getText(), "<font color='" + MATCH_COLOR + "'>" + regex.getText() + "</font>"); } else { replace += " " + cliente.getApMaterno(); } setText(replace); return this; } }