繁体   English   中英

如何在while循环中更新jLabel

[英]How to update a jLabel in a while loop

如何在while循环中更新jLabel? 我的理解是使用javax.swing.timer,但是我不太了解它,因为我还需要执行一个动作,现在这是一个while循环,需要在每次通过while循环时更新一个计数器。

有没有更简单的方法可以做到这一点,如果可以,我应该使用什么代替jLabel?

示例代码如下,我需要更新。

int rowCount = 0;
    while(rowIterator.hasNext())
    {


        jLabel5.setText(""+ rowCount); //<-- i need this to update
        myRow = sheet.getRow(count);
        cellIterator = myRow.cellIterator();
        Cell myCell2 = myRow.getCell(0);
        nextCell= myCell2.getStringCellValue();


        if(nextCell.equals(firstCell))
        {

            while(cellIterator.hasNext()) {

                            Cell cell = cellIterator.next();

                            switch(cell.getCellType()) {
                                case Cell.CELL_TYPE_BOOLEAN:
                                    System.out.print(cell.getBooleanCellValue() + "\t\t");
                                    break;
                                case Cell.CELL_TYPE_NUMERIC:
                                    cell.setCellType(Cell.CELL_TYPE_STRING);

                                     System.out.print(cell.getStringCellValue()+",");

                                    //System.out.print(cell.getStringCellValue() + "\t\t");
                                    writer.write(cell.getStringCellValue()+",");
                                    break;
                                case Cell.CELL_TYPE_STRING:
                                    System.out.print(cell.getStringCellValue()+",");
                                    //System.out.print(cell.getStringCellValue() + "\t\t");
                                    writer.write(cell.getStringCellValue()+",");
                                    break;
                            }
                        }
            System.out.println();
            writer.newLine();
            count++;
            rowCount++;



        }
        else
        {          

            writer.close();
            myRow = sheet.getRow(count);
            myCell2= myRow.getCell(0);
            nextCell=myCell2.getStringCellValue();
            firstCell=nextCell;
            Matter = "Matter Number: "+firstCell;
            num = firstCell;
            System.out.println(Matter);
            fWriter = new FileWriter(new File(directory, num+"_"+curdate+"_"+curtime+".csv"));
            writer = new BufferedWriter(fWriter);
            writer.write(Matter);
            writer.newLine();
            writer.write(header);
            writer.newLine();
        }


    }
}
catch (Exception e)
{
}

您遇到的问题是Swing是单线程环境。 也就是说,只有一个线程负责调度所有事件并处理所有重绘请求。

您所做的任何阻止该线程的操作都将阻止它更新UI(或响应新事件),使您的UI看起来像挂了一样。

您遇到的另一个问题是,对UI的所有更新仅应在该线程的上下文内进行(您不应尝试从另一个线程更新UI)。

在您的情况下, javx.swing.Timer将无济于事,因为计时器会在固定间隔内“滴答”,您需要一些可用于回调EDT来执行所需更新的内容。

在这种情况下,建议您使用SwingWorker

你可以看看

对于一些例子和想法

如果要显示所有值,则

String temp = "";
while(i < 100) {
     temp += "" + i;
     jLabel1.setText(temp);
     i++;
}

如果要显示最后一个值,则显示简单内容

while(i < 100) {
     jLabel1.setText(i);
     i++;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM