0

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)
{
}
4

2 に答える 2

3

あなたが抱えている問題は、Swingがシングルスレッド環境であることです。つまり、すべてのイベントのディスパッチとすべての再描画要求の処理を担当する単一のスレッドがあります。

このスレッドをブロックする操作を行うと、UI の更新 (または新しいイベントへの応答) が妨げられ、UI がハングしているように見えます。

もう 1 つの問題は、UI のすべての更新は、このスレッドのコンテキスト内でのみ実行する必要があることです (別のスレッドから UI を更新しようとしないでください)。

あなたのケースでjavx.swing.Timerは、タイマーが一定の間隔で「カチカチ」と音を立てるため、EDTにコールバックして必要な更新を実行するために使用できるものが必要です。

この場合、使用することをお勧めしますSwingWorker

ご覧いただけます

いくつかの例とアイデアについて

于 2013-03-06T09:06:50.440 に答える
0

すべての値を表示したい場合

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

最後の値を表示したい場合は、単純なコンテンツを表示します

while(i < 100) {
     jLabel1.setText(i);
     i++;
}
于 2013-03-06T08:41:08.660 に答える