Advertisement
stevennathaniel

AbstractTableModel Menggunakan jTable dari Swing Designer

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