problem in selecting cell in JTable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • san1014
    New Member
    • Jul 2007
    • 37

    problem in selecting cell in JTable

    Hi to all'
    I am using Java swings.
    I written a code to display a table. In the table i used Boolean.class type column.
    when i click the checkbox it value to be change from false to true.
    please help me . iam sending my code here

    CODE:

    package classes;

    import javax.swing.tab le.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event. *;
    import java.sql.*;
    import javax.swing.Def aultListModel;

    import classes.*;


    public class Test extends JFrame/* implements ActionListener*/ {
    public static void main(String[] args){
    form();
    }
    public static void form() {
    JFrame frame = new JFrame("Patient table");
    frame.getConten tPane().add(new JScrollPane(new JTable(new MyTableModel()) ));
    Container cont=frame.getC ontentPane();
    JPanel p= new JPanel();
    p.setLayout( new FlowLayout( FlowLayout.CENT ER ) );
    JButton saveb= new JButton("Save") ;
    JButton deleb= new JButton("Delete ");
    p.add(saveb);
    p.add(deleb);
    cont.add(p, BorderLayout.SO UTH);
    frame.pack();
    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
    frame.setVisibl e(true);
    final MyTableModel tb= new MyTableModel();
    deleb.addAction Listener(new ActionListener( ) {
    public void actionPerformed (ActionEvent ae) {

    for (int r=0;r<tb.i ;r++ )
    {

    Boolean s= (Boolean)tb.get ValueAt(r, 0);
    System.out.prin tln(s);
    /*if((boolean)tb .getValueAt(r,0 ))
    {
    try
    {

    Connection con = null;
    Statement stmt=null;
    ResultSet rs= null;
    ConnectionBean cb = new ConnectionBean( );
    con=cb.getConne ction();
    String id=(String)tb.g etValueAt(r,1);
    stmt=con.create Statement();
    int n= stmt.executeUpd ate("delete from patient_list where patient_id = "+ id);
    if(n>0)
    System.out.prin tln("row deleted");
    // frame.setVisibl e(false);
    form();

    }
    catch(Exception e)
    {
    }*/
    }


    }


    //}
    });

    }//end of form()
    }

    class MyTableModel extends AbstractTableMo del/*implements TableCellEditor */{
    int i;
    private String columnNames[] = new String[] { "Select" ,"ID","Name" , "Age","Sex" , "Address"," App Type"};

    private Class columnClasses[] = new Class[]{Boolean.class, String.class, String.class, String.class, String.class, String.class, String.class};
    private Object data[][];

    JComponent component = new JTextField();


    public int getColumnCount( ) {
    return columnNames.len gth;
    }

    public int getRowCount() {
    return data.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
    return data[rowIndex][columnIndex];
    }

    public Class getColumnClass( int columnIndex) {
    return columnClasses[columnIndex];
    }

    public String getColumnName(i nt columnIndex) {
    return columnNames[columnIndex];
    }

    public boolean isCellEditable( int rowIndex, int columnIndex) {
    /*if (columnIndex!=0 )
    return true;
    else
    return false;
    for(int j=0;j<i;j++)
    {
    if(isCellSelect ed(rowIndex, columnIndex))
    {
    System.out.prin tln("Hellooooo" );
    setValueAt(new Boolean(true), rowIndex, columnIndex);
    }
    }*/


    return (columnIndex==0 );
    }

    public void setValueAt(Obje ct aValue, int rowIndex, int columnIndex) {
    data[rowIndex][columnIndex] = aValue;
    fireTableCellUp dated(rowIndex, columnIndex);
    }



    /*//*************** **********
    private class CheckBoxCellEdi tor extends AbstractCellEdi tor implements TableCellEditor {
    protected JCheckBox checkBox;

    public CheckBoxCellEdi tor() {
    checkBox = new JCheckBox();
    checkBox.setHor izontalAlignmen t(SwingConstant s.CENTER);
    checkBox.setBac kground( Color.white);
    }

    public Component getTableCellEdi torComponent(
    JTable table,
    Object value,
    boolean isSelected,
    int row,
    int column) {

    checkBox.setSel ected(((Boolean ) value).booleanV alue());

    Component c = table.getDefaul tRenderer(Strin g.class).getTab leCellRendererC omponent(table, value, isSelected, false, row, column);
    if (c != null) {
    checkBox.setBac kground(c.getBa ckground());
    }

    return checkBox;
    }
    public Object getCellEditorVa lue() {
    return Boolean.valueOf (checkBox.isSel ected());
    }
    }*/


    MyTableModel()
    {
    try
    {
    Connection con = null;
    Statement stmt=null;
    ResultSet rs= null;
    ConnectionBean cb = new ConnectionBean( );
    con=cb.getConne ction();
    stmt= con.createState ment(ResultSet. TYPE_SCROLL_SEN SITIVE, ResultSet. CONCUR_UPDATABL E);
    rs= stmt.executeQue ry("Select * from patient_list");


    i=0;
    while(rs.next() )
    {
    i++;
    }
    //JCheckBox c[]= new JCheckBox[i];
    data= new Object[i][7];
    rs.beforeFirst( );
    int r=0;
    while(rs.next() )
    {
    data[r][0]=new Boolean(false); ;
    data[r][1]=rs.getString(1 );
    data[r][2]= rs.getString(2) ;
    data[r][3]=rs.getString(3 );
    data[r][4]=rs.getString(4 );
    data[r][5]=rs.getString(5 );
    data[r][6]= rs.getString(6) ;
    r++;
    }


    }
    catch (Exception e)
    {
    System.out.prin tln("Error Occurred: "+e.getMessage( ));
    }
    /*JFrame f= new JFrame();
    Container cont= f.getContentPan e();
    JPanel p= new JPanel();
    p.setLayout(new FlowLayout(Flow Layout.CENTER)) ;
    JButton saveb= new JButton("Save") ;
    JButton deleb= new JButton("Delete ");
    p.add(saveb);
    p.add(deleb);
    cont.add(p, BorderLayout.SO UTH);
    f.setSize(300, 200);
    //f.setVisible(tr ue);*/

    }







    ///new end ***//
    }
Working...