Advertisement
stevennathaniel

Membuat Checkbox Di Atas JTable (Permulaan)

Sep 14th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.46 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 latihan17;
  7.  
  8. import javax.swing.SwingUtilities;
  9.  
  10. import javax.swing.JTable;
  11.  
  12. import java.sql.Connection;
  13.  
  14. import java.sql.DriverManager;
  15.  
  16. import java.sql.ResultSet;
  17.  
  18. import java.sql.ResultSetMetaData;
  19.  
  20. import java.sql.Statement;
  21.  
  22. import java.util.logging.Level;
  23.  
  24. import java.util.logging.Logger;
  25.  
  26. import javax.swing.table.DefaultTableModel;
  27.  
  28. import java.sql.*;
  29.  
  30. import java.util.Locale;
  31.  
  32. import java.util.Vector;
  33.  
  34. import java.text.SimpleDateFormat;
  35.  
  36. import java.util.Calendar;
  37.  
  38. import com.toedter.calendar.JDateChooser;
  39.  
  40. import java.text.DateFormat;
  41.  
  42. import java.util.Date;
  43.  
  44. import java.awt.event.*;
  45.  
  46. import java.awt.Event;
  47.  
  48. import java.awt.*;
  49.  
  50. import java.util.Locale.Builder;
  51. import javax.swing.JCheckBox;
  52. import javax.swing.JLabel;
  53.  
  54. import javax.swing.table.TableCellRenderer;
  55.  
  56. import javax.swing.table.TableCellEditor;
  57.  
  58. import javax.swing.table.AbstractTableModel;
  59.  
  60. import javax.swing.table.DefaultTableColumnModel;
  61.  
  62. import javax.swing.table.*;
  63.  
  64.  
  65.  
  66.  
  67. /**
  68.  *
  69.  * @author steven
  70.  */
  71. public class TabelCheckBox1 extends javax.swing.JFrame {
  72.    
  73.    
  74.  
  75.          
  76.  
  77.  
  78.     /**
  79.      * Creates new form TabelCheckBox1
  80.      */
  81.     public TabelCheckBox1() {
  82.        
  83.         initComponents();
  84.        
  85.    //    jTable1.getCellRenderer(Boolean.class,A);
  86.        
  87.        // private static final Object[] namaKolom = new Object[]{
  88.            
  89. //        };
  90.                
  91.        
  92.        
  93.         // DefaultTableModel tabel1 = new DefaultTableModel(isiKolom, namaKolom);
  94.        
  95.         jTable1.setModel(new javax.swing.table.DefaultTableModel(
  96.             new Object [][] {
  97.                 {null, null, null, true},
  98.                 {null, null, null, null},
  99.                 {null, null, null, null},
  100.                 {null, null, null, null}
  101.             },
  102.             new String [] {
  103.                 "Nomer", "Nama", "Alamat", "Check List"
  104.             }
  105.         ));
  106.                
  107.       // jTable1.getColumnModel().getColumn(4).setCellRenderer(Boolean.class, jTable1.getDefaultRenderer(Boolean.class));
  108.  
  109.        
  110.         class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {
  111.    
  112.     // public class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {
  113.  
  114.           CheckBoxRenderer() {
  115.             setHorizontalAlignment(JLabel.CENTER);
  116.           }
  117.  
  118.           public Component getTableCellRendererComponent(JTable table, Object value,
  119.               boolean isSelected, boolean hasFocus, int row, int column) {
  120.             if (isSelected) {
  121.               setForeground(table.getSelectionForeground());
  122.               //super.setBackground(table.getSelectionBackground());
  123.               setBackground(table.getSelectionBackground());
  124.             } else {
  125.               setForeground(table.getForeground());
  126.               setBackground(table.getBackground());
  127.             }
  128.             setSelected((value != null && ((Boolean) value).booleanValue()));
  129.             return this;
  130.           }
  131. }
  132.        
  133.        
  134.         CheckBoxRenderer render = new CheckBoxRenderer();
  135.        
  136.        
  137.         // jTable1.getDefaultRenderer(Boolean.class);
  138.        
  139.         jTable1.getColumnModel().getColumn(3).setCellRenderer(render);
  140.  
  141. // Referensi : http://stackoverflow.com/questions/4154802/how-to-render-a-checkbox-in-a-jtable
  142.  
  143. }
  144.    
  145.  
  146.  
  147.        
  148.  
  149.     /**
  150.      * This method is called from within the constructor to initialize the form.
  151.      * WARNING: Do NOT modify this code. The content of this method is always
  152.      * regenerated by the Form Editor.
  153.      */
  154.     @SuppressWarnings("unchecked")
  155.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  156.     private void initComponents() {
  157.  
  158.         jScrollPane1 = new javax.swing.JScrollPane();
  159.         jTable1 = new javax.swing.JTable();
  160.  
  161.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  162.  
  163.         jTable1.setModel(new javax.swing.table.DefaultTableModel(
  164.             new Object [][] {
  165.                 {null, null, null, null},
  166.                 {null, null, null, null},
  167.                 {null, null, null, null},
  168.                 {null, null, null, null}
  169.             },
  170.             new String [] {
  171.                 "Title 1", "Title 2", "Title 3", "Title 4"
  172.             }
  173.         ));
  174.         jScrollPane1.setViewportView(jTable1);
  175.  
  176.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  177.         getContentPane().setLayout(layout);
  178.         layout.setHorizontalGroup(
  179.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  180.             .addGroup(layout.createSequentialGroup()
  181.                 .addContainerGap()
  182.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
  183.                 .addContainerGap(254, Short.MAX_VALUE))
  184.         );
  185.         layout.setVerticalGroup(
  186.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  187.             .addGroup(layout.createSequentialGroup()
  188.                 .addContainerGap()
  189.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
  190.                 .addContainerGap(13, Short.MAX_VALUE))
  191.         );
  192.  
  193.         pack();
  194.     }// </editor-fold>                        
  195.  
  196.     /**
  197.      * @param args the command line arguments
  198.      */
  199.     public static void main(String args[]) {
  200.         /* Set the Nimbus look and feel */
  201.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  202.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  203.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  204.          */
  205.         try {
  206.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  207.                 if ("Nimbus".equals(info.getName())) {
  208.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  209.                     break;
  210.                 }
  211.             }
  212.         } catch (ClassNotFoundException ex) {
  213.             java.util.logging.Logger.getLogger(TabelCheckBox1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  214.         } catch (InstantiationException ex) {
  215.             java.util.logging.Logger.getLogger(TabelCheckBox1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  216.         } catch (IllegalAccessException ex) {
  217.             java.util.logging.Logger.getLogger(TabelCheckBox1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  218.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  219.             java.util.logging.Logger.getLogger(TabelCheckBox1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  220.         }
  221.         //</editor-fold>
  222.  
  223.         /* Create and display the form */
  224.         java.awt.EventQueue.invokeLater(new Runnable() {
  225.             public void run() {
  226.                 new TabelCheckBox1().setVisible(true);
  227.             }
  228.         });
  229.     }
  230.  
  231.     // Variables declaration - do not modify                    
  232.     private javax.swing.JScrollPane jScrollPane1;
  233.     private javax.swing.JTable jTable1;
  234.     // End of variables declaration                  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement