获取最后一列最后一个单元格值 [英] getting last column last cell value

查看:116
本文介绍了获取最后一列最后一个单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了JTable,表中的值是从第五列中的数据库(第五列除外)中检索的.i在第五列中通过键盘输入值但是当时从jtable再次获取值时我没有得到最后一个在第五栏输入值请提出建议我的方法代码如下:/ b $ b

  public   class  TableDataDelete实现ActionListener 
{
public void actionPerformed(ActionEvent ee){


int b = table.getRowCount();
系统。 out .println( row算 + b);

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

尝试
{

String str =( String )table.getModel()。getValueAt(i, 3 );
String str1 =( String )table.getModel()。getValueAt(i, 5 );
if (!(str1 == null )){
((DefaultTableModel) )table.getModel())removeRow(ⅰ)。
table.repaint();
系统。 out .println( 删除行号是 + i);
}

}
catch (ArrayIndexOutOfBoundsException e)
{System。 out .println( 数组索引超出范围的异常 + e );
}

}

}
}

解决方案

< blockquote>如果你使用

((DefaultTableModel)table.getModel())。removeRow(i); 

,总行数将会改变。所以你必须修改计数值:

  int  b = table.getRowCount(); 
系统。 out .println( row算 + b);

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

尝试
{

String str =( String )table.getModel()。getValueAt(i, 3 );
String str1 =( String )table.getModel()。getValueAt(i, 5 );
if (!(str1 == null )){
((DefaultTableModel) )table.getModel())removeRow(ⅰ)。
b - = 1 ; // 如果删除1行,则减去1;不需要增加i
table.repaint();
系统。 out .println( 删除行号是 + i);
}

}
else {
i ++;
}
catch (ArrayIndexOutOfBoundsException e)
{System。 out .println( 数组索引超出范围的异常 + e);
}

}


请在此处阅读:



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



它描述了你在这次运行中的所有任务。



玩得开心!


在一天之后我有这个解决方案
< 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( 命中计数为 + count1);
count1 ++;
String str1 =( String )table.getModel()。getValueAt(i, 5 );
System.out.println( second value + model.getValueAt(i,< span class =code-digit> 5 ));
if (!(str1 == null)){

// ((DefaultTableModel)table.getModel())。removeRow(i);
model.removeRow(i);
i--;
table.repaint();
System.out.println( 删除行号是 + i);
}
}

}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println( 数组索引超出范围的异常 + e);
}

}
}
}
}


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

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);
                }

            }

}
    }

解决方案

if you use

((DefaultTableModel)table.getModel()).removeRow(i);

, the total row count will be changed. so you have to modify the count value:

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);
                }

            }


Please read here:

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

it describes all your tasks in this run.

Have fun!


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);
                }

            }
        }
}
    }


这篇关于获取最后一列最后一个单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆