Jtable con Checkboxs

Si en alguna oportunidad has necesitado poner checkbox en un Jtable y no tubiste ni idea aquí veremos lo fácil que resulta.
Todo va dentro de un arreglo estático pero Uds pueden ponerlo en un for while u otra estructura de control antes de lanzar el agregado o la función getTable()
Adjunto el código:
/** 
* 
* @author http://jonathan-palomino.blogspot.com/ 
* 
*/ 
import javax.swing.SwingUtilities; 
import java.awt.BorderLayout; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 
import javax.swing.UIManager;
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableModel;
public class J_table extends JFrame {
    private static final long serialVersionUID = 1L; 
    private JPanel jContentPane = null; 
    private JScrollPane scroll = null; 
    private JTable Table = null; 
    Object[] columnas = {"ACTIVOS", "NOMBRE", "EDAD"}; 
    Object[][] datos = { 
        {true,"JOSE",50}, 
        {false,"ANDREA",12}, 
        {true,"IVETTE",18}, 
        {false,"JUAN",18}};
    /** 
     * This method initializes scroll    
     *     
     * @return javax.swing.JScrollPane    
     */ 
    private JScrollPane getScroll() { 
        if (scroll == null) { 
            scroll = new JScrollPane(); 
            scroll.setViewportView(getTable()); 
        } 
        return scroll; 
    }
    /** 
     * This method initializes Table    
     *     
     * @return javax.swing.JTable    
     */ 
    private JTable getTable() { 
        if (Table == null) { 
             DefaultTableModel modelo = new DefaultTableModel(datos, columnas) { 
                    public Class getColumnClass(int column) { 
                        return getValueAt(1, column).getClass(); 
                    } 
                }; 
            Table = new JTable(modelo); 
           
        } 
        return Table; 
    }
    /** 
     * @param args 
     */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        SwingUtilities.invokeLater(new Runnable() { 
            public void run() { 
                try{ 
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
                }catch(Exception e) { 
                    e.printStackTrace(); 
                } 
                J_table thisClass = new J_table(); 
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                thisClass.setVisible(true); 
            } 
        }); 
    }
    /** 
     * This is the default constructor 
     */ 
    public J_table() { 
        super(); 
        initialize(); 
    }
    /** 
     * This method initializes this 
     * 
     * @return void 
     */ 
    private void initialize() { 
        this.setSize(609, 281); 
        this.setContentPane(getJContentPane()); 
        this.setTitle("JTable con checkbox por http://jonathan-palomino.blogspot.com/"); 
        this.setLocationRelativeTo(null); 
    }
    /** 
     * This method initializes jContentPane 
     * 
     * @return javax.swing.JPanel 
     */ 
    private JPanel getJContentPane() { 
        if (jContentPane == null) { 
            jContentPane = new JPanel(); 
            jContentPane.setLayout(new BorderLayout()); 
            jContentPane.add(getScroll(), BorderLayout.CENTER); 
        } 
        return jContentPane; 
    }
} 
Share:

3 comentarios:

Visitantes

Flag Counter

Popular Posts

Etiquetas

Recent Posts