Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package filebrowser;
  2.  
  3.  
  4.  
  5. import java.awt.*;
  6. import javax.swing.*;
  7. import javax.swing.border.EmptyBorder;
  8. import javax.swing.border.Border;
  9. import javax.swing.filechooser.FileSystemView;
  10. import java.io.File;
  11. import java.io.FileFilter;
  12.  
  13. /**
  14. This code uses a JList in two forms (layout orientation vertical & horizontal wrap) to
  15. display a File[]. The renderer displays the file icon obtained from FileSystemView.
  16. */
  17. class FileList {
  18.  
  19. public Component getGui(File[] all, boolean vertical) {
  20. // put File objects in the list..
  21. JList fileList = new JList(all);
  22. // ..then use a renderer
  23. fileList.setCellRenderer(new FileRenderer(!vertical));
  24.  
  25. if (!vertical) {
  26. fileList.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
  27. fileList.setVisibleRowCount(-1);
  28. } else {
  29. fileList.setVisibleRowCount(9);
  30. }
  31. return new JScrollPane(fileList);
  32. }
  33.  
  34. public static void main(String[] args) {
  35. SwingUtilities.invokeLater(new Runnable() {
  36. public void run() {
  37. File f = new File(System.getProperty("user.home"));
  38. //File f= new File();
  39. FileList fl = new FileList();
  40. Component c1 = fl.getGui(f.listFiles(new TextFileFilter()),true);
  41.  
  42. //f = new File(System.getProperty("user.home"));
  43. Component c2 = fl.getGui(f.listFiles(new TextFileFilter()),false);
  44.  
  45. JFrame frame = new JFrame("File List");
  46. JPanel gui = new JPanel(new BorderLayout());
  47. gui.add(c1,BorderLayout.WEST);
  48. gui.add(c2,BorderLayout.CENTER);
  49. c2.setPreferredSize(new Dimension(375,100));
  50. gui.setBorder(new EmptyBorder(3,3,3,3));
  51.  
  52. frame.setContentPane(gui);
  53. frame.pack();
  54. frame.setLocationByPlatform(true);
  55. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  56. frame.setVisible(true);
  57. }
  58. });
  59. }
  60. }
  61.  
  62. class TextFileFilter implements FileFilter {
  63.  
  64. public boolean accept(File file) {
  65. // implement the logic to select files here..
  66. String name = file.getName().toLowerCase();
  67. //return name.endsWith(".java") || name.endsWith(".class");
  68. return name.length()<20;
  69. }
  70. }
  71.  
  72. class FileRenderer extends DefaultListCellRenderer {
  73.  
  74. private boolean pad;
  75. private Border padBorder = new EmptyBorder(3,3,3,3);
  76.  
  77. FileRenderer(boolean pad) {
  78. this.pad = pad;
  79. }
  80.  
  81. @Override
  82. public Component getListCellRendererComponent(
  83. JList list,
  84. Object value,
  85. int index,
  86. boolean isSelected,
  87. boolean cellHasFocus) {
  88.  
  89. Component c = super.getListCellRendererComponent(
  90. list,value,index,isSelected,cellHasFocus);
  91. JLabel l = (JLabel)c;
  92. File f = (File)value;
  93. l.setText(f.getName());
  94. l.setIcon(FileSystemView.getFileSystemView().getSystemIcon(f));
  95. if (pad) {
  96. l.setBorder(padBorder);
  97. }
  98.  
  99. return l;
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement