{"id":905,"date":"2010-06-28T20:10:37","date_gmt":"2010-06-28T19:10:37","guid":{"rendered":"http:\/\/sahits.ch\/blog\/?p=905"},"modified":"2010-10-31T07:49:22","modified_gmt":"2010-10-31T06:49:22","slug":"image-gallery-with-swing","status":"publish","type":"post","link":"http:\/\/sahits.ch\/blog\/blog\/2010\/06\/28\/image-gallery-with-swing\/","title":{"rendered":"Image Gallery with Swing"},"content":{"rendered":"<p>I have a list of list of images which I would like to display so I can decide what to do with them. The structure of List of List is because similar images compose the inner list. The images of the inner list should be displayed in a row whereas the outer list are the different rows.<!--more--><br \/>\nThe result looks like this:<br \/>\n<a href=\"http:\/\/sahits.ch\/blog\/wp-content\/uploads\/2010\/06\/Screenshot-Gallery.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sahits.ch\/blog\/wp-content\/uploads\/2010\/06\/Screenshot-Gallery-300x273.png\" alt=\"\" title=\"Screenshot-Gallery\" width=\"300\" height=\"273\" class=\"alignright size-medium wp-image-906\" srcset=\"http:\/\/sahits.ch\/blog\/wp-content\/uploads\/2010\/06\/Screenshot-Gallery-300x273.png 300w, http:\/\/sahits.ch\/blog\/wp-content\/uploads\/2010\/06\/Screenshot-Gallery.png 825w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/a> To simplify the matter I used the JAI library for image display with my own subclass of DisplayJAI to load and resize the image.<\/p>\n<pre>\r\n\/\/ compute the number of columns since not all rows have \r\n\/\/ the same amount\r\nint maxCols = 0;\r\nfor (List&lt;String&gt; list : fileNames) {\r\n\tif (list.size()&gt;maxCols){\r\n\t\tmaxCols=list.size();\r\n\t}\r\n}\r\nJFrame frame = new JFrame(\"Gallery\");\r\nframe.setSize(600, 600);\r\n\r\nif (fileNames.size()&gt;0){\r\n\tContainer container = new JPanel();\r\n\tcontainer.setLayout(new GridLayout(fileNames.size(), maxCols));\r\n\tfor (List&lt;String&gt; list : fileNames) {\r\n\t\tfor (final String fileName : list) {\r\n\t\t\tfinal JPanel pane = new JPanel(new BorderLayout());\r\n\t\t\ttry {\r\n\t\t\t\tfinal SourcedDisplayJAI disp = new SourcedDisplayJAI(fileName, 300);\r\n\t\t\t\tRenderedImage img = ImageIO.read(new File(fileName));\r\n\t\t\t\tint height =img.getHeight();\r\n\t\t\t\tint width = img.getWidth();\r\n\t\t\t\tStringBuffer lbltext = new StringBuffer(fileName.substring(bp.length()+1));\r\n\t\t\t\tlbltext.append(\" (\").append(width).append(\", \").append(height).append(\")\");\r\n\t\t\t\tfinal JLabel lbl = new JLabel(lbltext.toString());\r\n\t\t\t\tFont font = lbl.getFont();\r\n\t\t\t\tfont = font.deriveFont(6);\r\n\t\t\t\tlbl.setFont(font);\r\n\t\t\t\tpane.add(lbl, BorderLayout.NORTH);\r\n\t\t\t\tpane.add(disp, BorderLayout.CENTER);\r\n\t\t\t\tfinal JButton btn = new JButton(\"Delete\");\r\n\t\t\t\tfinal JButton btnDisinct = new JButton(\"Distinct\");\r\n\t\t\t\tbtn.addActionListener(new ActionListener() {\r\n\t\t\t\t\t\r\n\t\t\t\t\t@Override\r\n\t\t\t\t\tpublic void actionPerformed(ActionEvent e) {\r\n\t\t\t\t\t\tif (updater.removeSimilarImage(fileName)){\r\n\t\t\t\t\t\t\tbtn.setVisible(false);\r\n\t\t\t\t\t\t\tdisp.setVisible(false);\r\n\t\t\t\t\t\t\tlbl.setVisible(false);\r\n\t\t\t\t\t\t\tbtnDisinct.setVisible(false);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t});\r\n\t\t\t\tJPanel south = new JPanel(new GridLayout(1, 2));\r\n\t\t\t\tsouth.add(btn);\r\n\t\t\t\tbtnDisinct.addActionListener(new ActionListener() {\r\n\t\t\t\t\t\r\n\t\t\t\t\t@Override\r\n\t\t\t\t\tpublic void actionPerformed(ActionEvent e) {\r\n\t\t\t\t\t\tif (updater.declareFileDifferent(fileName)){\r\n\t\t\t\t\t\t\tbtn.setVisible(false);\r\n\t\t\t\t\t\t\tdisp.setVisible(false);\r\n\t\t\t\t\t\t\tlbl.setVisible(false);\r\n\t\t\t\t\t\t\tbtnDisinct.setVisible(false);\t\t\t\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t});\r\n\t\t\t\tsouth.add(btnDisinct);\r\n\t\t\t\tpane.add(south, BorderLayout.SOUTH);\r\n\r\n\t\t\t\tcontainer.add(pane);\r\n\t\t\t} catch (IllegalArgumentException e) {\r\n\t\t\t\tcontainer.add(new JLabel());\r\n\t\t\t} catch (IOException e) {\r\n\t\t\t\t\/\/ file did not exist\r\n\t\t\t\tcontainer.add(new JLabel());\r\n\t\t\t}\r\n\t\t} \/\/ end iteration over files in row\r\n\t\tfor (int i = list.size();i&lt;maxCols;i++){\r\n\t\t\tcontainer.add(new JLabel());\r\n\t\t}\r\n\t}\r\n\t\r\n\tJScrollPane spane = new JScrollPane(container);\r\n\tframe.getContentPane().add(spane, BorderLayout.CENTER);\r\n} else {\r\n\tJLabel lbl = new JLabel(\"There are no similar files\");\r\n\tframe.getContentPane().add(lbl, BorderLayout.CENTER);\r\n}\r\n\r\n\r\nframe.setVisible(true);\r\nframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r\nframe.pack();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have a list of list of images which I would like to display so I can decide what to do with them. The structure of List of List is because similar images compose the inner list. The images of the inner list should be displayed in a row whereas the outer list are the &hellip; <a href=\"http:\/\/sahits.ch\/blog\/blog\/2010\/06\/28\/image-gallery-with-swing\/\" class=\"more-link\"><span class=\"screen-reader-text\">\u201eImage Gallery with Swing\u201c <\/span>weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[138,7,6],"tags":[113,63,132,301,154],"class_list":["post-905","post","type-post","status-publish","format-standard","hentry","category-it","category-java","category-programmieren","tag-en","tag-image","tag-jai","tag-java","tag-swing"],"_links":{"self":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/comments?post=905"}],"version-history":[{"count":5,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/905\/revisions"}],"predecessor-version":[{"id":938,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/905\/revisions\/938"}],"wp:attachment":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/media?parent=905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/categories?post=905"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/tags?post=905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}