【问题标题】:How to update a jLabel in a while loop如何在while循环中更新jLabel
【发布时间】:2013-03-06 07:37:57
【问题描述】:

如何在 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)
{
}

【问题讨论】:

    标签: java apache-poi jlabel auto-update


    【解决方案1】:

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

    您所做的任何阻止此线程的操作都会阻止它更新 UI(或响应新事件),从而使您的 UI 看起来像是挂起。

    您遇到的另一个问题是,对 UI 的所有更新都只能在此线程的上下文中执行(您不应该尝试从另一个线程更新 UI)。

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

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

    你可以看看

    一些例子和想法

    【讨论】:

      【解决方案2】:

      如果你想显示所有值,那么

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

      如果你想显示最后一个值,那么简单的显示内容

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

      【讨论】:

      • 这行不通,它会阻止 EDT 并阻止它处理任何重绘请求,直到循环(及其包含的方法)存在之后
      • 这是不可能的,基本上我已经尝试过,就我而言,需要某种睡眠,以便循环内的文本更新。例如,每次循环通过时,jLable 上的文本必须更改,因此对于 int,它必须逐字显示标签中发生的计数。
      • 所以,你打电话给setText,它会向 EDT 提出更新 UI 的请求,并在未来某个时间更新 UI,但与此同时,你继续循环,阻止 EDT 处理重绘请求并发生。最糟糕的是,您建议在 EDT 上“睡觉”,或者更糟糕的是,从 EDT 外部更新 UI。
      • 我建议你阅读Concurrency in Swing
      猜你喜欢
      • 1970-01-01
      • 2012-08-03
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 2021-03-17
      相关资源
      最近更新 更多