Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i create JTable in that table value are retrieve from data base (Except fifth column) in fifth column .i enter value through keyboard in fifth column but when getting value again from jtable at that time i am not getting last entered value in fifth column please suggest something my method code bellow
C#
public class TableDataDelete implements ActionListener
    {
        public void actionPerformed(ActionEvent ee) {
 
          
            int b = table.getRowCount();
            System.out.println("row count" + b);

            for (int i = 0; i <b; i++) {

                try
                {

                String str = (String) table.getModel().getValueAt(i, 3);
                String str1 = (String) table.getModel().getValueAt(i, 5);
                if (!(str1 == null)) {
                    ((DefaultTableModel)table.getModel()).removeRow(i);
                    table.repaint();
                    System.out.println("remove row no is"+i);
}

                }
                catch(ArrayIndexOutOfBoundsException e)
                {System.out.println("array index out of bound exception"+e);
                }

            }

}
    }
Posted
Updated 26-Mar-14 3:12am
v2

if you use
((DefaultTableModel)table.getModel()).removeRow(i);
, the total row count will be changed. so you have to modify the count value:
C#
int b = table.getRowCount();
            System.out.println("row count" + b);

            for (int i = 0; i <b;) {

                try
                {

                String str = (String) table.getModel().getValueAt(i, 3);
                String str1 = (String) table.getModel().getValueAt(i, 5);
                if (!(str1 == null)) {
                    ((DefaultTableModel)table.getModel()).removeRow(i);
                    b -= 1; // minus 1 if remove 1 row; not need to increase i
                    table.repaint();
                    System.out.println("remove row no is"+i);
}

                }
                else {
                   i++;
                }
                catch(ArrayIndexOutOfBoundsException e)
                {System.out.println("array index out of bound exception"+e);
                }

            }
 
Share this answer
 
Please read here:

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html[^]

it describes all your tasks in this run.

Have fun!
 
Share this answer
 
Java
after ony day i have got this solution
<pre lang="cs">public class TableDataDelete implements ActionListener
    {
        public void actionPerformed(ActionEvent ee) {
            int count=0;
            // int a = table.getColumnCount();
            if(table.getCellEditor() != null)
            {
                int count1=0;
                table.getCellEditor().stopCellEditing();
            int b = table.getRowCount();
            System.out.println("row count" + b);
            DefaultTableModel model = (DefaultTableModel)table.getModel();
            for (int i = 0; i < model.getRowCount(); i++) {

                try
                {
                     if(table.getCellEditor(i, 5) != null)
                    {

                System.out.println("hit count is "+count1);
                count1++;
                String str1 = (String) table.getModel().getValueAt(i, 5);
            System.out.println("second value"+model.getValueAt(i,5));
if (!(str1 == null)) {

//                  ((DefaultTableModel)table.getModel()).removeRow(i);
                    model.removeRow(i);
                     i--;
                    table.repaint();
                    System.out.println("remove row no is"+i);
}
                }

                }
                catch(ArrayIndexOutOfBoundsException e)
                {
                    System.out.println("array index out of bound exception"+e);
                }

            }
        }
}
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900