Advertisement
Guest User

JTable image display

a guest
Jun 28th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package proba;
  6.  
  7. import java.awt.Component;
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JLabel;
  10. import javax.swing.JTable;
  11. import javax.swing.table.DefaultTableCellRenderer;
  12.  
  13. /**
  14.  *
  15.  * @author soksok
  16.  */
  17. public class TablaFrame extends javax.swing.JFrame {
  18.  
  19.     /**
  20.      * Creates new form TablaFrame
  21.      */
  22.     public TablaFrame() {
  23.         initComponents();
  24.     }
  25.  
  26.     /**
  27.      * This method is called from within the constructor to initialize the form.
  28.      * WARNING: Do NOT modify this code. The content of this method is always
  29.      * regenerated by the Form Editor.
  30.      */
  31.     @SuppressWarnings("unchecked")
  32.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  33.     private void initComponents() {
  34.  
  35.         jScrollPane1 = new javax.swing.JScrollPane();
  36.         jTable1 = new javax.swing.JTable();
  37.  
  38.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  39.  
  40.         jTable1.setModel(new javax.swing.table.DefaultTableModel(
  41.             new Object [][] {
  42.                 {null, null, null, null},
  43.                 {null, null, null, null},
  44.                 {null, null, null, null},
  45.                 {null, null, null, null}
  46.             },
  47.             new String [] {
  48.                 "Title 1", "Title 2", "Title 3", "Title 4"
  49.             }
  50.         ));
  51.         jTable1.setCellSelectionEnabled(true);
  52.         jScrollPane1.setViewportView(jTable1);
  53.  
  54.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  55.         getContentPane().setLayout(layout);
  56.         layout.setHorizontalGroup(
  57.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  58.             .addGroup(layout.createSequentialGroup()
  59.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
  60.                 .addGap(0, 25, Short.MAX_VALUE))
  61.         );
  62.         layout.setVerticalGroup(
  63.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  64.             .addGroup(layout.createSequentialGroup()
  65.                 .addContainerGap()
  66.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
  67.                 .addContainerGap(13, Short.MAX_VALUE))
  68.         );
  69.  
  70.         pack();
  71.     }// </editor-fold>
  72.  
  73.     /**
  74.      * @param args the command line arguments
  75.      */
  76.     public static void main(String args[]) {
  77.         /*
  78.          * Set the Nimbus look and feel
  79.          */
  80.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  81.         /*
  82.          * If Nimbus (introduced in Java SE 6) is not available, stay with the
  83.          * default look and feel. For details see
  84.          * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  85.          */
  86.         try {
  87.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  88.                 if ("Nimbus".equals(info.getName())) {
  89.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  90.                     break;
  91.                 }
  92.             }
  93.         } catch (ClassNotFoundException ex) {
  94.             java.util.logging.Logger.getLogger(TablaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  95.         } catch (InstantiationException ex) {
  96.             java.util.logging.Logger.getLogger(TablaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  97.         } catch (IllegalAccessException ex) {
  98.             java.util.logging.Logger.getLogger(TablaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  99.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  100.             java.util.logging.Logger.getLogger(TablaFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  101.         }
  102.         //</editor-fold>
  103.  
  104.         /*
  105.          * Create and display the form
  106.          */
  107.         java.awt.EventQueue.invokeLater(new Runnable() {
  108.  
  109.             public void run() {
  110.                 new TablaFrame().setVisible(true);
  111.                 jTable1.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());              
  112.  
  113.             }
  114.         });
  115.     }
  116.     // Variables declaration - do not modify
  117.     private javax.swing.JScrollPane jScrollPane1;
  118.     public static javax.swing.JTable jTable1;
  119.     // End of variables declaration
  120. static class ImageRenderer extends DefaultTableCellRenderer {
  121.       JLabel lbl = new JLabel();
  122.       ImageIcon icon = null;
  123.        
  124.       ImageRenderer() {
  125.           icon = new ImageIcon(getClass().getResource("/home/soksok/windows7.jpg"));
  126.       }
  127.        
  128.       @Override
  129.       public Component getTableCellRendererComponent(JTable table, Object value,
  130.                                                      boolean isSelected, boolean hasFocus,  
  131.                                                      int row, int column) {                  
  132.         lbl.setText((String)value);
  133.         lbl.setIcon(icon);
  134.                  
  135.         return lbl;
  136.       }
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement