Results 1 to 15 of 15
  1. #1
    pranav13's Avatar
    pranav13 is offline Member
    Join Date
    Oct 2008
    Location
    Greater Hyderabad
    Posts
    37
    Rep Power
    0

    Question [SOLVED] Form is loading slowly! :(

    In NetBeans IDE, I have created a form in which i fetch some data from a text file and put it in a textpanel. For this i have written a function and called it from the constructor of my class. My problem is that, the form is taking much time (4 - 7 seconds) to load.

    I have used the following code to get data from a text file which is in the jar.
    getData()
    {
    InputStream is = this.getClass().getResourceAsStream("License.txt") ;
    while((chr = is.read()) != -1) {
    str+=((char) chr);
    }
    is.close();
    jTextPane1.setText(str);
    }

    I have called the above function from constructor of my class as follows...
    public Step2()
    {
    initcomponents();
    getData();
    jTextPane1.setCaretPosition(0);
    }


    please tell me wheather this way of getting text from a file is correct or not. And suggest me the correct way to do this.
    And also tell me where shud i call the getData() function.

    thanks in advance!
    Thanks and Regards,
    Pranav

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    On the constructor you initiate the GUI as well as start to read the text file as well. It's a bad approach actually. Loading time can cause of the file size. So do those process separately, may be on a button click event or something.

    To see the different comment the line getData() on the constructor and run the application.

  3. #3
    pranav13's Avatar
    pranav13 is offline Member
    Join Date
    Oct 2008
    Location
    Greater Hyderabad
    Posts
    37
    Rep Power
    0

    Exclamation

    yes... the form is loading quickly!

    but, i need to display the license agreement in the text panel when the form is loaded. i dont want to click any button or anything else to display the file.

    pls tell me from where should i call the function.
    Thanks and Regards,
    Pranav

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Simple answer: read it in a background thread.
    For more see Simple Background Tasks and How to Use Progress Bars.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  6. #6
    pranav13's Avatar
    pranav13 is offline Member
    Join Date
    Oct 2008
    Location
    Greater Hyderabad
    Posts
    37
    Rep Power
    0

    Default

    thanks for ur suggestions!
    can u pls tell me a way for that? i mn using swingworker or sth lse?
    im new to java. this is my first project.
    Thanks and Regards,
    Pranav

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  8. #8
    pranav13's Avatar
    pranav13 is offline Member
    Join Date
    Oct 2008
    Location
    Greater Hyderabad
    Posts
    37
    Rep Power
    0

    Lightbulb

    i wanna run the process which gets data from file in background.
    For this, i came to know abt SwingWorker.
    pls tell me a process to do tht!
    Thanks and Regards,
    Pranav

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Form where you find it. I'm not familiar with such a thing Java.

    To run a process in background you need to use a thread. That's all. So you need to study about threads.

  10. #10
    pranav13's Avatar
    pranav13 is offline Member
    Join Date
    Oct 2008
    Location
    Greater Hyderabad
    Posts
    37
    Rep Power
    0

    Lightbulb

    herez da link for SwingWorker class...

    java.sun.com/products/jfc/tsc/articles/threads/threads2.html]Using a Swing Worker Thread

    ofcourse, its based on Threads!
    Thanks and Regards,
    Pranav

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  12. #12
    pranav13's Avatar
    pranav13 is offline Member
    Join Date
    Oct 2008
    Location
    Greater Hyderabad
    Posts
    37
    Rep Power
    0

    Default

    stuffthathappens.com/blog/2007/09/22/surprising-swingworker-behavior/
    here is a simple way to use SwingWorker.
    I am using jdk1.5 in netbeans6. swingworker is included in jdk6. how can i use this in java5?
    i tried to compile the SwingWorker.java and placed the class file in my project. but invain.
    Thanks and Regards,
    Pranav

  13. #13
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I haven't used SwingWorker since j2se 1.4. It was a separate, non-api class we could download then. I'm unable to find it at Sun now. Seems it gets revised every now and then. It was useful for work that was complex enough to be built into its own class and could provide updates to the ui, eg, progressBar updates, or callbacks to other processes. I haven't tried the j2se 1.6 version yet.
    It is usually pretty easy to design things without SwingWorker.
    Here's an idea:
    Java Code:
    import java.awt.*;
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    
    public class BackgroundWork {
        JTextArea textArea;
    
        private JScrollPane getTextComponent() {
            textArea = new JTextArea();
            textArea.setMargin(new Insets(5,5,5,5));
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            readFile();
            return new JScrollPane(textArea);
        }
    
        private void readFile() {
            // Start a background thread to read a file
            // and write to the textArea.
            Thread thread = new Thread(runner);
            // Keep gui responsive to user input.
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
            // This method can return now to allow the gui
            // construction to go ahead while "runner" does
            // its work in the background.
        }
    
        private Runnable runner = new Runnable() {
            public void run() {
                // I don't have a big-enough file to read so let's
                // try reading your thread page over the network.
                String path = "http://www.java-forums.org/awt-swing/" +
                              "12502-form-loading-slowly.html";
                try {
                    URLConnection connection = new URL(path).openConnection();
                    InputStream is = connection.getInputStream();
                    BufferedReader br = new BufferedReader(
                                        new InputStreamReader(is));
                    textArea.read(br, connection);
                    br.close();   
                } catch(IOException e) {
                    System.out.println("read error: " + e.getMessage());
                }
            }
        };
    
        public static void main(String[] args) {
            BackgroundWork test = new BackgroundWork();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getTextComponent(), "Center");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes me too, not familiar with such a tool. Again I advice you to use threads for that. Look at the hardwired code above.

  15. #15
    pranav13's Avatar
    pranav13 is offline Member
    Join Date
    Oct 2008
    Location
    Greater Hyderabad
    Posts
    37
    Rep Power
    0

    Default

    thank you very much hardwired.
    ur code helped me alot!
    Thanks and Regards,
    Pranav

Similar Threads

  1. enctype=multipart/form-data with form data in struts
    By vk_satheesh in forum New To Java
    Replies: 0
    Last Post: 09-19-2008, 12:48 PM
  2. loading JInternalFrames
    By vishakha in forum AWT / Swing
    Replies: 5
    Last Post: 07-23-2008, 03:58 PM
  3. loading a new Jframe
    By Ebylord in forum New To Java
    Replies: 0
    Last Post: 07-22-2008, 08:31 PM
  4. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 09:33 AM
  5. Help Loading Up Pictures
    By marco in forum Java Applets
    Replies: 3
    Last Post: 08-14-2007, 10:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •