如何在while循环中更新jLabel?我的理解是使用javax.swing.timer,但我不太了解它,因为我需要执行一个动作,现在这是一个while循环,需要在每次通过while循环时更新一个计数器。如何在while循环中更新jLabel
有没有更简单的方法来做到这一点,如果是这样,我应该用什么来代替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)
{
}
这不是去工作,它会阻止教育署T,并阻止它处理任何重绘请求,直到循环(及其包含的方法)存在之后 – MadProgrammer 2013-03-06 09:01:18
这是不可能的,基本上我试过要做的事情,那就需要有一种睡眠,只要我有关使得文本在循环内更新。例如,每当循环通过时,文本必须在jLable上更改,因此对于int,它必须从字面上显示标签中发生的计数。 – Silentdarkness 2013-03-06 09:05:00
因此,您调用'setText',它会在EDT上提出请求以更新UI并在将来某个时间,但同时,您将继续循环,防止EDT处理重新绘制请求并发生。最糟糕的是,你建议在美国东部时区“睡觉”,甚至更糟糕,从美国东部时间以外更新用户界面。 – MadProgrammer 2013-03-06 09:06:33