Results 1 to 15 of 15
- 10-16-2008, 08:16 AM #1
[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
- 10-16-2008, 09:00 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 10-16-2008, 09:14 AM #3
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
- 10-16-2008, 09:27 AM #4
Simple answer: read it in a background thread.
For more see Simple Background Tasks and How to Use Progress Bars.
- 10-16-2008, 09:45 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes the best solution is using threads. But doing it separately not much effect.
- 10-16-2008, 12:04 PM #6
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
- 10-16-2008, 12:16 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-16-2008, 02:07 PM #8
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
- 10-16-2008, 02:32 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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-16-2008, 03:43 PM #10
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
- 10-16-2008, 06:12 PM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, it's all about threads. Better to read that page first of all.
- 10-17-2008, 07:29 AM #12
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
- 10-19-2008, 07:49 AM #13
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); } }
- 10-20-2008, 10:52 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes me too, not familiar with such a tool. Again I advice you to use threads for that. Look at the hardwired code above.
- 10-20-2008, 12:51 PM #15
Similar Threads
-
enctype=multipart/form-data with form data in struts
By vk_satheesh in forum New To JavaReplies: 0Last Post: 09-19-2008, 12:48 PM -
loading JInternalFrames
By vishakha in forum AWT / SwingReplies: 5Last Post: 07-23-2008, 03:58 PM -
loading a new Jframe
By Ebylord in forum New To JavaReplies: 0Last Post: 07-22-2008, 08:31 PM -
Loading Images - Imp
By Thulasiraman in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 09:33 AM -
Help Loading Up Pictures
By marco in forum Java AppletsReplies: 3Last Post: 08-14-2007, 10:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks