Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Disappearing applet, where's it going?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Disappearing applet, where's it going?

    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);
                }
            }
        }


  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Disappearing applet, where's it going?

    I don't see that I can delete this thread, so I'm just replying (for future reference maybe) and marking solved for now - I think the problem is related to the web host. I'm seeing a lot of caching problems in the Java console.

Similar Threads

  1. [SOLVED] Grass disappearing :(
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2010, 09:09 PM
  2. applet img
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: April 7th, 2010, 09:14 PM
  3. applet menubar
    By tabutcher in forum Java Applets
    Replies: 2
    Last Post: March 5th, 2010, 09:30 AM
  4. [SOLVED] applet vs. gui app
    By rptech in forum AWT / Java Swing
    Replies: 3
    Last Post: August 27th, 2009, 09:13 AM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM