Ok, here's hoping someone more experienced than I, can explain this to me.
I have an applet, it's not terribly complicated. It runs exactly the way it should, in terms of logic and output. There's just this one thing:
Part of the applet's function is to read an Excel file. To do this, I open a file chooser, the user picks a file, and I use the Apache POI functions to read the file into an ArrayList. The problem is that, while the import is going on, the applet completely disappears from the browser window. It disappears for somewhere around 20 seconds, then reappears.
Initially, I thought this was because it was a single-threaded applet, that couldn't redraw itself while it did the import. This didn't make a lot of sense, since my test Excel file was only 15 rows and took a millisecond to import, which couldn't explain the 20 second blanking of the applet in the browser.
Since then, I've made the import process into a separate thread. I've put repaint()s and validate()s right after starting the thread. I've done pretty much everything I can think of, but I can't get it to avoid the 20 second blanking.
Note: the blanking only happens on a "fresh" run of the code. On subsequent imports, even of different Excel files, there is no delay. It happens in Safari, Firefox and Windows Explorer. It doesn't happen on local execution, only when run from a website.
Anyone have a thought about this?
Code that calls the import thread:
JFileChooser fileopen = new JFileChooser(); //create the file chooser FileFilter filter = new FileNameExtensionFilter("Excel files", "xls", "xlsx"); //add acceptable extensions fileopen.addChoosableFileFilter(filter); //add filter to the chooser int ret = fileopen.showDialog(null, "Open"); //show the chooser, w/default buttons selected OPEN if (ret != JFileChooser.APPROVE_OPTION) { //if they choose something besides OK return; //(ie, cancel) return to interface wait-state } File filename = fileopen.getSelectedFile(); fis = null; //attempt to read the file, catch exceptions try { fis = new FileInputStream(filename); } catch (FileNotFoundException ex) { Logger.getLogger(creatorApplet.class.getName()).log(Level.SEVERE, null, ex); } //get extension of the file, so we can use the right importer //this is for future use, as we only accept 1 type of file now, but //will be useful later when we expand to .csv and .doc String extension; int dot = filename.toString().lastIndexOf("."); //find the last dot extension = filename.toString().substring(dot + 1, filename.toString().length()); //take everything from the last dot to the end if (extension.equals( "xls") || extension.equals("xlsx")) { readExcelFile r = new readExcelFile(); Thread t = new Thread(r); t.start(); while (t.isAlive()) { System.out.println("Waiting on thread"); // just to prove it's in here } } else { System.out.println("Huge, mostly impossible error"); }
Code that does the actual import:
public class readExcelFile implements Runnable { public void run() { int lastColumn; int lastRow; HSSFWorkbook workbook = null; try { workbook = new HSSFWorkbook(fis); } catch (IOException ex) { Logger.getLogger(creatorApplet.class.getName()).log(Level.SEVERE, null, ex); } HSSFSheet sheet = workbook.getSheetAt(0); //find the first sheet Iterator rows = sheet.rowIterator(); //get the maximum column number, based on the header row; columns that have blank //header rows won't be included in the import lastColumn = (sheet.getRow(0).getLastCellNum()); lastRow = (sheet.getLastRowNum()); //loop through each row of the sheet for (short rowC = 0; rowC <= lastRow; rowC++) { Row row = sheet.getRow(rowC); //read the entire row List data = new ArrayList(); //then step through it cell by cell for (short cellC = 0; cellC < lastColumn; cellC++) { Cell cell = row.getCell(cellC); if (cell == null) { //if the cell is null, I still need an entry, so add a blank instead data.add(""); } else { //otherwise, just add what's in the cell data.add(cell.getStringCellValue()); } } sheetData.add(data); //add the data list reprenting the row to the the database } try { fis.close(); } catch (IOException ex) { System.out.println(ex); } } }