0

How do i update a jLabel in a while loop? My understanding is to use a javax.swing.timer however i dont quite understand it because i need an action performed as well, now this is in a while loop that needs to update a counter to every time it passes through the while loop.

Is there an easier way to do this and if so what should i use instead of a jLabel?

Sample code is below of what i need to update.

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)
{
}
Silentdarkness
  • 461
  • 1
  • 10
  • 30

2 Answers2

3

The problem you have is Swing is a single threaded enironment. That is, there is a single thread responsible for dispatching all the events and processing all the repaint requests.

Anything you do that blocks this thread, will prevent it from updating the UI (or responding to new events) making your UI look like it's hung.

The other problem you have is that all updates to the UI should only ever be carried out within the context of this thread (you shouldn't try updating the UI from another thread).

In your case a javx.swing.Timer isn't going to help, as a timer "ticks" at a regular interval, you need something that you can use to call back to the EDT to perform the required updates.

In this case, I suggest you use SwingWorker

You can have a look at

For some examples and ideas

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

If you want to show all values then

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

If you want to show last value then simple show content

while(i < 100) {
     jLabel1.setText(i);
     i++;
}
Dhinakar
  • 4,061
  • 6
  • 36
  • 68
  • 1
    That's not going to work, it's going to block the EDT and prevent it from processing any repaint requests until AFTER the loop (and the method it's contained in) exists – MadProgrammer Mar 06 '13 at 09:01
  • That's not possible, basically what i have tried to do, there need to be some kind of a sleep as far as i'm concerned so that the text updates within the loop. For example each time the loop passes the text must change on the jLable, so for an int it must literally show the count happening in the label. – Silentdarkness Mar 06 '13 at 09:05
  • So, you call `setText`, it puts a request on the EDT to update the UI and some time in the future, but in the mean time, you continue looping, preventing the EDT from processing that repaint request and occurring. Worst, you suggesting "sleeping" on the EDT or even worse, updating the UI from outside the EDT. – MadProgrammer Mar 06 '13 at 09:06
  • 1
    I suggest you have a read of [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) – MadProgrammer Mar 06 '13 at 09:07