{"id":1267,"date":"2015-01-26T12:32:45","date_gmt":"2015-01-26T11:32:45","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=1267"},"modified":"2015-01-26T12:32:45","modified_gmt":"2015-01-26T11:32:45","slug":"java-jlist-items-with-icon","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=1267","title":{"rendered":"Java &#8211; JList items with icon"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"897\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=897\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/eclipse.png\" data-orig-size=\"44,42\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Java\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/eclipse.png\" class=\" size-full wp-image-897 alignleft\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/eclipse.png\" alt=\"Java\" width=\"44\" height=\"42\" \/>In this article I&#8217;ll present a code snippet how to implement a <em>JList<\/em> in <em>Java <\/em>where items will be displayed like [icon] [item text] .<!--more--><br \/>\nLet&#8217;s have an example where list item contains simple text and a flag if the item is active or not. We will display green light If item is active and red light if it&#8217;s not active.<\/p>\n<pre lang=\"java\">class ListItem {\r\n  public boolean isActive;\r\n  public String label;\r\n\r\n  public ListItem(String label, boolean isActive) {\r\n    this.label = label;\r\n    this.isActive = isActive;\r\n  }\r\n}<\/pre>\n<p>Next we have a JFrame class which encapsulates our simple application<\/p>\n<pre lang=\"java\">class MyApplication extends JFrame {\r\n  private JList itemList;\r\n \r\n  public MyApplication() {\r\n    DefaultListModel listModel = new DefaultListModel();\t\t\r\n    \r\n    itemList = new JList();\r\n    itemList.setModel((ListModel) listModel);\r\n    itemList.setCellRenderer(new IconLabelListRenderer());\r\n    this.loadItemList();\r\n  }\r\n\r\n  private void loadItemList() {\r\n    DefaultListModel listModel = (DefaultListModel)this.itemList.getModel();\r\n    listModel.removeAllElements();\r\n\r\n    for (int i = 1; i &lt;= 5; i++) {\r\n\/\/    add five items where every second item will be set active (i%2==0)\r\n      listModel.addElement(new ListItem(\"Item \" + i, i%2 == 0));\r\n  }\r\n}<\/pre>\n<p>&#8230;and implement your custom list cell renderer:<\/p>\n<pre lang=\"java\">public class IconLabelListRenderer extends DefaultListCellRenderer {\r\n  private JLabel label;\r\n  private final String RED_LIGHT = \"\/resources\/icons\/red_light.png\";\r\n  private final String GREEN_LIGHT = \"\/resources\/icons\/green_light.png\";\r\n\r\n  public IconLabelListRenderer(){\r\n    label = new JLabel();\r\n    label.setOpaque(true);\r\n  }\r\n\r\n  @Override\r\n  public Component getListCellRendererComponent(\r\n                     JList&lt;? extends Object&gt; list, \r\n                     Object value, \r\n                     int index, \r\n                     boolean selected, \r\n                     boolean expanded) {\r\n    ListItem item = (ListItem)value;\r\n\t\t\r\n    if(item.isActive) \r\n      this.label.setIcon(new ImageIcon(\r\n        MyApplication.class.getResource(this.GREEN_LIGHT)\r\n      ));\r\n    else \r\n      this.label.setIcon(new ImageIcon(\r\n        MyApplication.class.getResource(this.RED_LIGHT)\r\n      ));\r\n        \r\n    this.label.setText(item.label);\r\n    this.label.setToolTipText(item.label);\r\n    if (selected) {\r\n      label.setBackground(list.getSelectionBackground());\r\n      label.setForeground(list.getSelectionForeground());\r\n    } else {\r\n      label.setBackground(list.getBackground());\r\n      label.setForeground(list.getForeground());\r\n    }\r\n\r\n    return label;\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this article I&#8217;ll present a code snippet how to implement a JList in Java where items will be displayed like [icon] [item text] .<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Java - JList items with icon","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9,10],"tags":[380,381,113,379],"class_list":["post-1267","post","type-post","status-publish","format-standard","hentry","category-development","category-java","tag-defaultlistcellrenderer","tag-defaultlistmodel","tag-icon","tag-jlist"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-kr","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1267"}],"version-history":[{"count":2,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1267\/revisions"}],"predecessor-version":[{"id":1269,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1267\/revisions\/1269"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}