Código:
package gui; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.GregorianCalendar; import javax.swing.DefaultCellEditor; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.JTableHeader; import javax.swing.table.TableCellEditor; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; import calendario.DateChooser; import objects.Usuario; public class Cita extends JFrame{ private static final long serialVersionUID = 1L; public Cita() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(150, 150); start(); } private boolean DEBUG = false; //variables de instancia GregorianCalendar date = new GregorianCalendar(); Usuario user = new Usuario(); DateChooser dc = new DateChooser(this, date); //Paneles private JPanel pnl1; private JPanel pnl2; private JPanel pnl3; //Botones private JButton b1; private JButton b2; //labels private JLabel lbl1; private JLabel lbl2; private JLabel lbl3; //TextFields private JTextField txt1; private JTextField txt2; private JTextField txt3; //CheckBox JCheckBox chk1; JCheckBox chk2; //Table private JTable tabla1; JScrollPane scrollPane = new JScrollPane(tabla1); TableCellEditor editor; TableColumnModel columname = new DefaultTableColumnModel(); private void start() { pnl1 = new JPanel(); pnl2 = new JPanel(); pnl3 = new JPanel(); txt1 = new JTextField(); txt2 = new JTextField(); txt3 = new JTextField(); new JTextField(); b2 = new JButton("Fecha Cita"); b1 = new JButton("Agregar"); pnl3.setLayout(new GridLayout(10,1)); lbl1 = new JLabel(" Nombre"); lbl2 = new JLabel(" Apellido"); lbl3 = new JLabel(" Telefono"); new JLabel(" Hora/Fecha"); MyTableModel tm = new MyTableModel(); tabla1 = new JTable(tm); getContentPane().add(scrollPane, BorderLayout.CENTER); tabla1.setPreferredScrollableViewportSize(new Dimension(500, 70)); tabla1.setFillsViewportHeight(true); //Esta Vivo VIVOOOO!!!!! //esto sirve para que el valor booleano se vea como una checkbox tabla1.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(chk1 = new JCheckBox())); tabla1.getColumnModel().getColumn(5).setCellEditor(new DefaultCellEditor(chk2 = new JCheckBox())); } public static void main(String[] args) { Cita ct = new Cita(); ct.init(); ct.setVisible(true); ct.pack(); } private void init() { this.getContentPane().add(pnl1, BorderLayout.CENTER); pnl1.add(pnl2, BorderLayout.NORTH); pnl1.add(pnl3, BorderLayout.SOUTH); pnl2.setLayout(new BorderLayout()); pnl2.add(tabla1, BorderLayout.CENTER); //pnl2.add(tabla1.getTableHeader(), BorderLayout.NORTH); pnl3.add(lbl1); pnl3.add(txt1); pnl3.add(lbl2); pnl3.add(txt2); pnl3.add(lbl3); pnl3.add(txt3); //pnl3.add(lbl4); //pnl3.add(txt4); pnl3.add(b2); //accion del boton mostrar fecha. b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { if (dc.showDateChooser() == DateChooser.OK_OPTION) { System.out.println(dc.dateSelect); user.setCita(dc.dateSelect); } } }); pnl3.add(b1); //accion del boton agregar b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { user.setNombre(txt1.getText()); user.setApellido(txt2.getText()); user.setTelefono(txt3.getText()); System.out.println(user.toString()); } }); chk1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println(chk1.isSelected()); }}); chk2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println(chk1.isSelected()); }}); } //Abstract Table Model modelo de la trabla que sirve. private class MyTableModel extends AbstractTableModel { public MyTableModel() { } private String [] columns = {"Nombre", "Apellido", "Telefono","Fecha-Hora","Presente", "Emergencia" }; private Object [][] data = { {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, {"Josia", "Olivo", "Castillo", "14/07/2009", new Boolean(true),new Boolean(false)}, {"Josue", "Olivo", "Castillo", "14/07/2009", new Boolean(false),new Boolean(true)}, }; private static final long serialVersionUID = 1L; public int getColumnCount() { return columns.length; } public int getRowCount() { return data.length; } public Class<? extends Object> getColumnClass(int c) { return getValueAt(0, c).getClass(); } public Object getValueAt(int row, int col) { return data[row][col]; } public boolean isCellEditable(int row, int col) { //Note that the data/cell address is constant, //no matter where the cell appears onscreen. if (col < 2) { return false; } else { return true; } } public void setValueAt(Object value, int row, int col) { if (DEBUG) { System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")"); } data[row][col] = value; fireTableCellUpdated(row, col); if (DEBUG) { System.out.println("New value of data:"); printDebugData(); } } private void printDebugData() { int numRows = getRowCount(); int numCols = getColumnCount(); for (int i=0; i < numRows; i++) { System.out.print(" row " + i + ":"); for (int j=0; j < numCols; j++) { System.out.print(" " + data[i][j]); } System.out.println(); } System.out.println("--------------------------"); } } }