Results 1 to 3 of 3
  1. #1
    SmugMug is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default JFrame frozen while updating data from streams?

    Hello! I'm currently learning java as my first real language :)

    While learning to work with network streams, I ran into an unrelated problem. I wrote a simple program that pulls data down from a website and displays it in a couple of fields. I set up a JLabel to display the program's status throughout the course of the update, but the window just freezes for 2-3 seconds while the update takes place.

    Java Code:
    		try {
    			url = new URL("http://download.finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=sl1d1t1c1ohgv&e=.csv");
    			urlConn = url.openConnection();
    *			lblStatus.setText("Downloading Data");
    			inStream = new InputStreamReader(urlConn.getInputStream());
    			buff = new BufferedReader(inStream);
    
    			String csvString = buff.readLine();
    
    			StringTokenizer tokenizer = new StringTokenizer(csvString, ",");
    *			lblStatus.setText("Parsing");
    
    			txtpnSymbol.setText(tokenizer.nextToken().replaceAll("\"", ""));
    			txtpnPrice.setText(tokenizer.nextToken());
    			...
    The label field never updates, even when the update takes several seconds. For all intensive purposes, the window is frozen. I haven't covered multithreading yet, but is this because I need to have the update and UI operating on separate threads? I'm pretty stumped.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: JFrame frozen while updating data from streams?

    Time to "cover" multithreading I think, because that's the cause of your current problem. Your streaming is stomping on the Swing event thread (or EDT for Event Dispatch Thread), preventing it from doing its actions. Since the EDT is responsible for drawing all Swing graphics, including those of the GUI components, and all Swing user interactions, your application becomes effectively frozen if the EDT is bogged down with some long running task. The solution is to do the task in a background thread, and then updating the GUI on the event thread when necessary.

    Hang on a sec. Let me look up a link for you for the tutorial...

    Here it is. Please have a look at "Lesson: Concurrency in Swing". Then please come on back if you're still stuck.

  3. #3
    SmugMug is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: JFrame frozen while updating data from streams?

    Thank you, that is exactly the information I was looking for. That explains why the book I've been using as a reference uses the console instead of swing elements to show status updates with streams, because all of the programs I've written so far have been single-threaded. I'll have to make learning to multithread my next goal.

Similar Threads

  1. Best practices in handling big data streams
    By rsantana in forum Advanced Java
    Replies: 2
    Last Post: 08-11-2011, 04:23 PM
  2. Socket, streams and data types?
    By Tomtefaen in forum Networking
    Replies: 0
    Last Post: 01-15-2011, 12:36 AM
  3. Updating JFrame
    By shadowfax57 in forum AWT / Swing
    Replies: 1
    Last Post: 11-21-2010, 04:01 PM
  4. frozen program
    By rp181 in forum Networking
    Replies: 4
    Last Post: 10-11-2009, 06:25 AM

Posting Permissions

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