Advertisement
stevennathaniel

Final CheckBox AbstractTableModel di jTable Swing Design

Oct 21st, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.41 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.        
  77.         public void setValueAt(Object value, int row, int col){
  78.            
  79.             data[row][col] = value;
  80.            
  81.             super.setValueAt(value, row, col);
  82.         }
  83.        
  84.        
  85.         // @Override
  86.        
  87.         public int getColumnCount(){
  88.            
  89.             return namaKolom.length;
  90.        
  91.        
  92.     }
  93.        
  94.         public int getRowCount(){
  95.            
  96.             return data.length;
  97.         }
  98.        
  99.        
  100.         public String getColumnName(int col){
  101.            
  102.             return namaKolom[col];
  103.         }
  104.        
  105.        
  106.         public Object getValueAt(int row, int col){
  107.            
  108.             return data[row][col];
  109.         }
  110.        
  111.        
  112.         //public Class<?> getColumnClass(int col){
  113.            
  114.             // return data[0][col].getClass();
  115.            
  116.             // return data[0] [col].getClass();
  117.            
  118.           //  if(col == 1)
  119.                
  120.            // return Boolean.class;
  121.            
  122.           //  else
  123.                
  124.           //   return Object.class;
  125.         // }
  126.        
  127.        
  128.         public Class getColumnClass(int c){
  129.        
  130.         return getValueAt(1, c).getClass();
  131.     }
  132.        
  133.        
  134.         public boolean isCellEditable(int row, int col){
  135.            
  136.            
  137.             // sesuaikan angka kolom dengan angka kolom yg akan bisa di edit
  138.            
  139.             if(col == 2)
  140.                
  141.             return false;
  142.            
  143.             else
  144.            
  145.             return true;
  146.            
  147.            
  148.            
  149.         }
  150.    
  151.     }
  152.    
  153.    // class TabelPegawai extends AbstractTableModel{
  154.        
  155.        
  156.        
  157.        
  158.    
  159.    
  160.    
  161.    
  162.     public Tabel1() {
  163.         initComponents();
  164.        
  165.        
  166.         TabelPegawaiModel tabel1 = new TabelPegawaiModel();
  167.        
  168.         jTable1.setModel(tabel1);
  169.        
  170.        
  171.     }
  172.  
  173.     /**
  174.      * This method is called from within the constructor to initialize the form.
  175.      * WARNING: Do NOT modify this code. The content of this method is always
  176.      * regenerated by the Form Editor.
  177.      */
  178.     @SuppressWarnings("unchecked")
  179.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  180.     private void initComponents() {
  181.  
  182.         jScrollPane1 = new javax.swing.JScrollPane();
  183.         jTable1 = new javax.swing.JTable();
  184.  
  185.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  186.  
  187.         jTable1.setModel(new javax.swing.table.DefaultTableModel(
  188.             new Object [][] {
  189.                 {null, null, null, null},
  190.                 {null, null, null, null},
  191.                 {null, null, null, null},
  192.                 {null, null, null, null}
  193.             },
  194.             new String [] {
  195.                 "Title 1", "Title 2", "Title 3", "Title 4"
  196.             }
  197.         ));
  198.         jScrollPane1.setViewportView(jTable1);
  199.  
  200.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  201.         getContentPane().setLayout(layout);
  202.         layout.setHorizontalGroup(
  203.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  204.             .addGroup(layout.createSequentialGroup()
  205.                 .addContainerGap()
  206.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  207.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  208.         );
  209.         layout.setVerticalGroup(
  210.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  211.             .addGroup(layout.createSequentialGroup()
  212.                 .addContainerGap()
  213.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  214.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  215.         );
  216.  
  217.         pack();
  218.     }// </editor-fold>                        
  219.  
  220.     /**
  221.      * @param args the command line arguments
  222.      */
  223.     public static void main(String args[]) {
  224.         /* Set the Nimbus look and feel */
  225.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  226.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  227.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  228.          */
  229.         try {
  230.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  231.                 if ("Nimbus".equals(info.getName())) {
  232.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  233.                     break;
  234.                 }
  235.             }
  236.         } catch (ClassNotFoundException ex) {
  237.             java.util.logging.Logger.getLogger(Tabel1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  238.         } catch (InstantiationException ex) {
  239.             java.util.logging.Logger.getLogger(Tabel1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  240.         } catch (IllegalAccessException ex) {
  241.             java.util.logging.Logger.getLogger(Tabel1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  242.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  243.             java.util.logging.Logger.getLogger(Tabel1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  244.         }
  245.         //</editor-fold>
  246.  
  247.         /* Create and display the form */
  248.         java.awt.EventQueue.invokeLater(new Runnable() {
  249.             public void run() {
  250.                 new Tabel1().setVisible(true);
  251.             }
  252.         });
  253.     }
  254.  
  255.     // Variables declaration - do not modify                    
  256.     private javax.swing.JScrollPane jScrollPane1;
  257.     private javax.swing.JTable jTable1;
  258.     // End of variables declaration                  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement