Advertisement
stevennathaniel

AbstractTableModel Berhasil Tampil di jTable Swing Designer

Oct 20th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.44 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package latihan29;
  7.  
  8.  
  9. import javax.swing.DefaultCellEditor;
  10.  
  11. import javax.swing.JComboBox;
  12.  
  13. import javax.swing.JFrame;
  14.  
  15. import javax.swing.JPanel;
  16.  
  17. import javax.swing.JScrollPane;
  18.  
  19. import javax.swing.JTable;
  20.  
  21. import javax.swing.table.AbstractTableModel;
  22.  
  23. import javax.swing.table.DefaultTableCellRenderer;
  24.  
  25. import javax.swing.table.TableCellRenderer;
  26.  
  27. import javax.swing.table.TableColumn;
  28.  
  29. import java.awt.Component;
  30.  
  31. import java.awt.Dimension;
  32.  
  33. import java.awt.GridLayout;
  34.  
  35. /**
  36.  *
  37.  * @author steven
  38.  *
  39.  * Referensi Pendukung: http://kodejava.org/how-do-i-render-boolean-value-as-checkbox-in-jtable-component/
  40.  */
  41.  
  42.  
  43.  
  44. public class Tabel1 extends javax.swing.JFrame {
  45.  
  46.     /**
  47.      * Creates new form Tabel1
  48.      */
  49.    
  50.    
  51.     /**
  52.      * Menciptakan tabel model
  53.      *
  54.      */
  55.    
  56.    
  57.     class TabelPegawaiModel extends AbstractTableModel{
  58.        
  59.        
  60.         // super("Tabel Pegawai");
  61.        
  62.         String[] namaKolom = {"Nama Pegawai","CheckList"};
  63.        
  64.        
  65.         Object[][] data = {
  66.            
  67.            
  68.             {"Steven Nathaniel", new Boolean(true)},
  69.            
  70.             {"Bill Gates", new Boolean(true)}
  71.            
  72.            
  73.         };
  74.        
  75.        
  76.         // @Override
  77.        
  78.         public int getColumnCount(){
  79.            
  80.             return namaKolom.length;
  81.        
  82.        
  83.     }
  84.        
  85.         public int getRowCount(){
  86.            
  87.             return data.length;
  88.         }
  89.        
  90.        
  91.         public String getColumnName(int col){
  92.            
  93.             return namaKolom[0];
  94.         }
  95.        
  96.        
  97.         public Object getValueAt(int row, int col){
  98.            
  99.             return data[row][col];
  100.         }
  101.        
  102.        
  103.         public Class<?> getColumnClass(int col){
  104.            
  105.             return data[0][col].getClass();
  106.         }
  107.        
  108.     }
  109.    
  110.    // class TabelPegawai extends AbstractTableModel{
  111.        
  112.        
  113.        
  114.        
  115.    
  116.    
  117.    
  118.    
  119.     public Tabel1() {
  120.         initComponents();
  121.        
  122.        
  123.         TabelPegawaiModel tabel1 = new TabelPegawaiModel();
  124.        
  125.         jTable1.setModel(tabel1);
  126.        
  127.        
  128.     }
  129.  
  130.     /**
  131.      * This method is called from within the constructor to initialize the form.
  132.      * WARNING: Do NOT modify this code. The content of this method is always
  133.      * regenerated by the Form Editor.
  134.      */
  135.     @SuppressWarnings("unchecked")
  136.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  137.     private void initComponents() {
  138.  
  139.         jScrollPane1 = new javax.swing.JScrollPane();
  140.         jTable1 = new javax.swing.JTable();
  141.  
  142.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  143.  
  144.         jTable1.setModel(new javax.swing.table.DefaultTableModel(
  145.             new Object [][] {
  146.                 {null, null, null, null},
  147.                 {null, null, null, null},
  148.                 {null, null, null, null},
  149.                 {null, null, null, null}
  150.             },
  151.             new String [] {
  152.                 "Title 1", "Title 2", "Title 3", "Title 4"
  153.             }
  154.         ));
  155.         jScrollPane1.setViewportView(jTable1);
  156.  
  157.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  158.         getContentPane().setLayout(layout);
  159.         layout.setHorizontalGroup(
  160.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  161.             .addGroup(layout.createSequentialGroup()
  162.                 .addContainerGap()
  163.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  164.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  165.         );
  166.         layout.setVerticalGroup(
  167.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  168.             .addGroup(layout.createSequentialGroup()
  169.                 .addContainerGap()
  170.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  171.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  172.         );
  173.  
  174.         pack();
  175.     }// </editor-fold>                        
  176.  
  177.     /**
  178.      * @param args the command line arguments
  179.      */
  180.     public static void main(String args[]) {
  181.         /* Set the Nimbus look and feel */
  182.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  183.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  184.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  185.          */
  186.         try {
  187.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  188.                 if ("Nimbus".equals(info.getName())) {
  189.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  190.                     break;
  191.                 }
  192.             }
  193.         } catch (ClassNotFoundException ex) {
  194.             java.util.logging.Logger.getLogger(Tabel1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  195.         } catch (InstantiationException ex) {
  196.             java.util.logging.Logger.getLogger(Tabel1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  197.         } catch (IllegalAccessException ex) {
  198.             java.util.logging.Logger.getLogger(Tabel1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  199.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  200.             java.util.logging.Logger.getLogger(Tabel1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  201.         }
  202.         //</editor-fold>
  203.  
  204.         /* Create and display the form */
  205.         java.awt.EventQueue.invokeLater(new Runnable() {
  206.             public void run() {
  207.                 new Tabel1().setVisible(true);
  208.             }
  209.         });
  210.     }
  211.  
  212.     // Variables declaration - do not modify                    
  213.     private javax.swing.JScrollPane jScrollPane1;
  214.     private javax.swing.JTable jTable1;
  215.     // End of variables declaration                  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement