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 17 of 17

Thread: Program hangs

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program hangs

    when control gose to this method Application Freezes i want to show the results on the JTextField but as long as the program is downloading pages from links it remain still no output result shows in the text field
    // here i call this method downloadpage
     for (Element link : links) {
                print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));
               myField.setText(i+"%");
               downloadpage(link.attr("abs:href"),link.text());
     
     
     
            }
    here is the method code
     url  = new URL(Link);
        String name;
        name = "D:\\net\\"+Name+".html";
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        writer = new BufferedWriter(new FileWriter(name));
          String line;
          while ((line = reader.readLine()) != null) {
     
             writer.write(line);
             writer.newLine();
          }
          reader.close();
          writer.close();
    I'm a beginner Please Help Me


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program hangs

    Can you make a small complete program the compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program hangs

    i didn't check the code

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.filechooser.FileSystemView;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.JTableHeader;
    import javax.swing.table.TableColumn;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
     
    public class OWD implements ActionListener,ItemListener {
     
    JTextField textfield = new JTextField(15);
    public void gui(){
    JFrame downloader = new JFrame("Offline Web Page Downloader");
    JButton button = new JButoon("download");
    Container con1;
        con1 = downloader.getContentPane();
        con1.setLayout(new FlowLayout());
    JPanel mypanel = new JPanel();
    mypanel.add(button);
    mypanel.add(textfield);
    button.addActionListener(this);
    con1.add(mypanel);
    }
    public void actionPerformed(ActionEvent e){
    if(e.getSource == button)
    {
    	Document doc = Jsoup.connect(http://www.yahoo.com/).get();
            Elements links = doc.select("a[href]");
     
     
    int i = 1;
            for (Element link : links) {
     
               textfield.setText(i+"%");
               downloadpage(link.attr("abs:href"),link.text());
     
     
     
            }
     
          }
    }
    public void downloadpage(String Link,String Name)throws Exception{
        url  = new URL(Link);
        String name;
        name = "D:\\net\\"+Name+".html";
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        writer = new BufferedWriter(new FileWriter(name));
          String line;
          while ((line = reader.readLine()) != null) {
     
             writer.write(line);
             writer.newLine();
          }
          reader.close();
          writer.close();
     
    }
    OWD()
    {
    	gui();
    }
    public static void main(String args[]){
    	OWD od = new OWD();
    }
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Program hangs

    when control gose to this method Application Freezes i want to show the results on the JTextField but as long as the program is downloading pages from links it remain still no output result shows in the text field
    Swing is single threaded, managed by the event dispatch thread (EDT) - if you have to do work which takes an excessive amount of time and place this in the EDT the user interface will appear to freeze. The solution: use a worker Thread or SwingWorker

    Worker Threads and SwingWorker (The Java™ Tutorials > Creating a GUI With JFC/Swing > Concurrency in Swing)

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program hangs

    but if i use work thread then where i call the function it says that can not find symbol downloadpage(string,string)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program hangs

    To call a method in a class, you need a reference to that class. Pass a reference to the class with the method you are trying to call to the class where you are trying to call it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program hangs

    Quote Originally Posted by Norm View Post
    To call a method in a class, you need a reference to that class. Pass a reference to the class with the method you are trying to call to the class where you are trying to call it.
    And how can i do that
    sorry brother but as i said i'm a Biginner Thanks

    --- Update ---

    And both method are in same class

    --- Update ---

    here i'm calling the function all this code is in a same class called OWD
            int i = 1;
            for (Element link : links) {
                print(" * a: <%s>  (%s)", link.attr("abs:href"), trim(link.text(), 35));
               myField.setText(i+"%");
               downloadpage(link.attr("abs:href"),link.text()); // here the Compiler says cannot find  symbol downloadpge Location class OWD
     
     
     
            }
    this is how i'm doing is it correct
     SwingWorker worker = new SwingWorker() {
    public void downloadpage(String Link,String Name)throws Exception{
        url  = new URL(Link);
        String name;
        name = "D:\\net\\"+Name+".html";
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        writer = new BufferedWriter(new FileWriter(name));
          String line;
          while ((line = reader.readLine()) != null) {
     
             writer.write(line);
             writer.newLine();
          }
          reader.close();
          writer.close();
     
    }
     
        @Override
        protected Object doInBackground() throws Exception {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     };

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program hangs

    When you create the worker class, pass it a reference to the class with the method you want to call.

    I don't see where the methods and classes are from the snippets of code you have posted.
    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program hangs

    Please check it and tell me where is the error
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.filechooser.FileSystemView;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.JTableHeader;
    import javax.swing.table.TableColumn;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
     
    public class OWD implements ActionListener,ItemListener {
     
    JTextField textfield = new JTextField(15);
    public void gui(){
    JFrame downloader = new JFrame("Offline Web Page Downloader");
    JButton button = new JButoon("download");
    Container con1;
        con1 = downloader.getContentPane();
        con1.setLayout(new FlowLayout());
    JPanel mypanel = new JPanel();
    mypanel.add(button);
    mypanel.add(textfield);
    button.addActionListener(this);
    con1.add(mypanel);
    }
    public void actionPerformed(ActionEvent e){
    if(e.getSource == button)
    {
    	Document doc = Jsoup.connect(http://www.yahoo.com/).get();
            Elements links = doc.select("a[href]");
     
     
    int i = 1;
            for (Element link : links) {
     
               textfield.setText(i+"%");
               downloadpage(link.attr("abs:href"),link.text()); // here it says cannot find symbol download page 
     
     
     
            }
     
          }
    }
    SwingWorker worker = new SwingWorker() {
    public void downloadpage(String Link,String Name)throws Exception{
        url  = new URL(Link);
        String name;
        name = "D:\\net\\"+Name+".html";
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        writer = new BufferedWriter(new FileWriter(name));
          String line;
          while ((line = reader.readLine()) != null) {
     
             writer.write(line);
             writer.newLine();
          }
          reader.close();
          writer.close();
     
    }
        @Override
        protected Object doInBackground() throws Exception {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     };
    OWD()
    {
    	gui();
    }
    public static void main(String args[]){
    	OWD od = new OWD();
    }
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program hangs

    where is the error
    What error are you taking about? Can you explain?
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program hangs

    at line number 52 and 61 please check it says cannot find symbol downloadpage in class owd

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.filechooser.FileSystemView;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.JTableHeader;
    import javax.swing.table.TableColumn;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
     
    public class OWD implements ActionListener,ItemListener {
     
    JTextField textfield = new JTextField(15);
    public void gui(){
    JFrame downloader = new JFrame("Offline Web Page Downloader");
    JButton button = new JButoon("download");
    Container con1;
        con1 = downloader.getContentPane();
        con1.setLayout(new FlowLayout());
    JPanel mypanel = new JPanel();
    mypanel.add(button);
    mypanel.add(textfield);
    button.addActionListener(this);
    con1.add(mypanel);
    }
    public void actionPerformed(ActionEvent e){
    if(e.getSource == button)
    {
    	Document doc = Jsoup.connect(http://www.yahoo.com/).get();
            Elements links = doc.select("a[href]");
     
     
    int i = 1;
            for (Element link : links) {
     
               textfield.setText(i+"%");
               downloadpage(link.attr("abs:href"),link.text()); // here it says cannot find symbol download page 
     
     
     
            }
     
          }
    }
    SwingWorker worker = new SwingWorker() {
    public void downloadpage(String Link,String Name)throws Exception{ // here is the method which i'm calling above
        url  = new URL(Link);
        String name;
        name = "D:\\net\\"+Name+".html";
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        writer = new BufferedWriter(new FileWriter(name));
          String line;
          while ((line = reader.readLine()) != null) {
     
             writer.write(line);
             writer.newLine();
          }
          reader.close();
          writer.close();
     
    }
        @Override
        protected Object doInBackground() throws Exception {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     };
    OWD()
    {
    	gui();
    }
    public static void main(String args[]){
    	OWD od = new OWD();
    }
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program hangs

    Please copy the full text of the error message and paste it here. It has important info about the error.

    The code in post#11 has syntax errors and will NOT compile. Please post correct code.

    I don't have the jsoup packages. Can you make a program that does not use them for testing the problem about accessing a method?

    The swing worker should include ALL the code in the listener that requires time to execute.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program hangs

    Here at the line number 34 compiler generats an syntax error that "Can not find symbol download in class javafxapplication6;
    "

    package javafxapplication6;
     
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import javax.swing.SwingWorker;
     
     
    public class JavaFXApplication6 {
     
     
     
     
    SwingWorker worker = new SwingWorker(){    
        void download(){
            System.out.println("I'm downloading");
        }
     
        @Override
        protected Object doInBackground() throws Exception {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    };
        void run(){
            for(int i = 0;i<10;i++){
                download();
            }
        }
        public static void main(String[] args) {
     
        }
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program hangs

    The method download() is not in scope where it is being referenced. The code that calls download needs a copy of the worker variable that is a reference to the class that contains the download() method.
    I don't think placing the method download inside the SwingWorker definition puts the method in the class's definition.

    I don't think you are coding the SwingWorker class correctly. Look at the examples in the API doc and model your code after that.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program hangs

    I will appreciate your help if you make a small program for me

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Program hangs

    Look at the API doc for the SwingWorker class. It has some examples there on how to use the class.
    Java Platform SE 7

    Sorry, I don't write code.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program hangs

    Thanks

Similar Threads

  1. java program hangs when executed
    By ars42025 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 16th, 2013, 12:39 PM
  2. UI hangs while sending data on bluetooth.
    By tama in forum Android Development
    Replies: 5
    Last Post: August 28th, 2013, 08:35 AM
  3. Swing & Threading ... Systen hangs
    By russdb in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 3rd, 2012, 11:54 PM
  4. Network communication hangs
    By droyhull in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 8th, 2011, 10:55 AM
  5. Thread Hangs at split method.
    By kailasvilaskore in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 12:13 PM