104104 */
105105public class SwingSearchBar extends JTextField {
106106
107- public static final int ICON_SIZE = 32 ;
108-
109107 private static final String DEFAULT_MESSAGE = "Click here to search" ;
110108 private static final Color ACTIVE_FONT_COLOR = new Color (0 , 0 , 0 );
111109 private static final Color INACTIVE_FONT_COLOR = new Color (150 , 150 , 150 );
@@ -131,6 +129,9 @@ public class SwingSearchBar extends JTextField {
131129 /** The maximum number of results per search category. */
132130 private int resultLimit = 8 ;
133131
132+ /** The size of the search result icons (both width and height). */
133+ private int iconSize = 16 ;
134+
134135 /** Whether the selection should change upon mouseover. */
135136 private boolean mouseoverEnabled ;
136137
@@ -207,6 +208,12 @@ public void setResultLimit(final int resultLimit) {
207208 this .resultLimit = resultLimit ;
208209 }
209210
211+ /** Sets the size (both width and height) of the search result icons. */
212+ public void setIconSize (final int iconSize ) {
213+ if (iconSize < 0 ) return ; // Ignore invalid limit.
214+ this .iconSize = iconSize ;
215+ }
216+
210217 /** Gets whether the selection should change upon mouseover. */
211218 public boolean isMouseoverEnabled () {
212219 return mouseoverEnabled ;
@@ -685,7 +692,7 @@ private void rebuild() {
685692 }
686693
687694 private Component icon (final String iconPath ) {
688- if (iconPath == null || iconPath .isEmpty ()) return emptyIcon ();
695+ if (iconSize == 0 || iconPath == null || iconPath .isEmpty ()) return emptyIcon ();
689696 if (iconPath .startsWith ("http" )) {
690697 try {
691698 URL url = new URL (iconPath );
@@ -701,18 +708,18 @@ private Component icon(final String iconPath) {
701708 final URL iconURL = getClass ().getResource (iconPath );
702709 if (iconURL == null ) return emptyIcon ();
703710 ImageIcon icon = new ImageIcon (iconURL );
704- if (icon .getIconWidth () != ICON_SIZE || //
705- icon .getIconHeight () != ICON_SIZE )
711+ if (icon .getIconWidth () != iconSize || //
712+ icon .getIconHeight () != iconSize )
706713 {
707714 // Resize icon to the needed size.
708715 icon = new ImageIcon (icon .getImage ().getScaledInstance (
709- ICON_SIZE , ICON_SIZE , Image .SCALE_SMOOTH ));
716+ iconSize , iconSize , Image .SCALE_SMOOTH ));
710717 }
711718 return new JLabel (icon );
712719 }
713720
714721 private Component emptyIcon () {
715- return Box .createRigidArea (new Dimension (ICON_SIZE , ICON_SIZE ));
722+ return Box .createRigidArea (new Dimension (iconSize , iconSize ));
716723 }
717724
718725 private boolean isHeader (final SearchResult value ) {
0 commit comments