Image Gallery with Swing

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.
The result looks like this:
To simplify the matter I used the JAI library for image display with my own subclass of DisplayJAI to load and resize the image.

// compute the number of columns since not all rows have 
// the same amount
int maxCols = 0;
for (List<String> list : fileNames) {
	if (list.size()>maxCols){
		maxCols=list.size();
	}
}
JFrame frame = new JFrame("Gallery");
frame.setSize(600, 600);

if (fileNames.size()>0){
	Container container = new JPanel();
	container.setLayout(new GridLayout(fileNames.size(), maxCols));
	for (List<String> list : fileNames) {
		for (final String fileName : list) {
			final JPanel pane = new JPanel(new BorderLayout());
			try {
				final SourcedDisplayJAI disp = new SourcedDisplayJAI(fileName, 300);
				RenderedImage img = ImageIO.read(new File(fileName));
				int height =img.getHeight();
				int width = img.getWidth();
				StringBuffer lbltext = new StringBuffer(fileName.substring(bp.length()+1));
				lbltext.append(" (").append(width).append(", ").append(height).append(")");
				final JLabel lbl = new JLabel(lbltext.toString());
				Font font = lbl.getFont();
				font = font.deriveFont(6);
				lbl.setFont(font);
				pane.add(lbl, BorderLayout.NORTH);
				pane.add(disp, BorderLayout.CENTER);
				final JButton btn = new JButton("Delete");
				final JButton btnDisinct = new JButton("Distinct");
				btn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						if (updater.removeSimilarImage(fileName)){
							btn.setVisible(false);
							disp.setVisible(false);
							lbl.setVisible(false);
							btnDisinct.setVisible(false);
						}
					}
				});
				JPanel south = new JPanel(new GridLayout(1, 2));
				south.add(btn);
				btnDisinct.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						if (updater.declareFileDifferent(fileName)){
							btn.setVisible(false);
							disp.setVisible(false);
							lbl.setVisible(false);
							btnDisinct.setVisible(false);			
						}
					}
				});
				south.add(btnDisinct);
				pane.add(south, BorderLayout.SOUTH);

				container.add(pane);
			} catch (IllegalArgumentException e) {
				container.add(new JLabel());
			} catch (IOException e) {
				// file did not exist
				container.add(new JLabel());
			}
		} // end iteration over files in row
		for (int i = list.size();i<maxCols;i++){
			container.add(new JLabel());
		}
	}
	
	JScrollPane spane = new JScrollPane(container);
	frame.getContentPane().add(spane, BorderLayout.CENTER);
} else {
	JLabel lbl = new JLabel("There are no similar files");
	frame.getContentPane().add(lbl, BorderLayout.CENTER);
}


frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();

Schreibe einen Kommentar