Guest User

Untitled

a guest
Jan 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. static public ImageIcon createIcon(String path) {
  2. URL imgURL =Main.class.getResource(path);
  3. if (imgURL != null) {
  4. return new ImageIcon(imgURL);
  5. } else {
  6. System.err.println("File not found " + path);
  7. return null;
  8. }
  9. }
  10. static class ImageRenderer extends DefaultTableCellRenderer {
  11. JLabel lbl = new JLabel();
  12. ArrayList<String> s;
  13. public ImageRenderer( ArrayList<String> s) {
  14. this.s=s;
  15. }
  16.  
  17.  
  18. public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
  19. boolean hasFocus, int row, int column) {
  20. lbl.setText((String) value);
  21. ImageIcon icon = createIcon(s.get(row));
  22. lbl.setIcon(icon);
  23. return lbl;
  24. }
  25. }
  26. static JFrame frame = new JFrame();
  27.  
  28.  
  29.  
  30. static public void open(JFrame f, DefaultTableModel d, JTable t) {
  31.  
  32. FileDialog fd =new FileDialog(f, "Wczytaj",FileDialog.LOAD);
  33. fd.setVisible(true);
  34. String katalog=fd.getDirectory();
  35. String plik=fd.getFile();
  36.  
  37. BufferedReader br = null;
  38. try {
  39. br = new BufferedReader(new FileReader(katalog+plik));
  40. String line;
  41. int id_data=0;
  42. ArrayList<String> s = new ArrayList<String>();
  43. while ((line = br.readLine()) != null) {
  44.  
  45. String[] tmp=line.split("\s+");
  46.  
  47. //ImageIcon icon2 = new ImageIcon("java.png");
  48.  
  49. Object[] row= { tmp[0],tmp[1],tmp[2] };
  50. d.addRow(row);
  51.  
  52. s.add(katalog+tmp[3]+".png");
  53. // s[id_data]=s[id_data].replaceAll("", "\");
  54. //System.out.println(new File(s.get(id_data)).exists());
  55. // t.setValueAt(icon2, id_data, 3);
  56.  
  57. id_data++;
  58. }
  59. t.getColumnModel().getColumn(3).setCellRenderer(new ImageRenderer(s));
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. } finally {
  63. try {
  64. if (br != null) {
  65. br.close();
  66. }
  67. } catch (IOException ex) {
  68. ex.printStackTrace();
  69. }
  70. }
  71. }
Add Comment
Please, Sign In to add comment