1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
package swing.tests;
import java.awt.Color;
import java.awt.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class TestRenderer extends DefaultListCellRenderer {
/**
*
*/
private static final long serialVersionUID = 5134289471936146644L;
@Override
public Component getListCellRendererComponent(JList arg0, Object arg1,
int arg2, boolean arg3, boolean arg4) {
// TODO Auto-generated method stub
// JLabel l = (JLabel) super.getListCellRendererComponent(arg0, arg1,
// arg2, arg3, arg4);
this.setText(null);
this.setBackground(Color.white);
this.setIcon((ImageIcon) arg1);
this.setHorizontalTextPosition(SwingConstants.CENTER);
this.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(5,5,5,5),new LineBorder(Color.blue.brighter().brighter().brighter())));
return this;
}
public static void main(String[] args) {
List<ImageIcon> icons = new ArrayList<ImageIcon>();
File f;
f = new File("thumbs");
File[] ic = f.listFiles();
System.out.println(f.getPath());
System.out.println(f.getAbsolutePath());
System.out.println(f.listFiles().length);
for (File tmp : ic) {
icons.add(new ImageIcon(TestRenderer.class.getResource("/thumbs/"
+ tmp.getName())));
}
JList l = new JList(icons.toArray());
l.setCellRenderer(new TestRenderer());
l.setLayoutOrientation(JList.HORIZONTAL_WRAP);
JFrame frm = new JFrame();
frm.add(l);
frm.pack();
frm.setLocationRelativeTo(null);
frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frm.setVisible(true);
}
} |
Partager