usama8800
usama8800

Reputation: 875

Getting webpage source code without freezing UI

I am trying to get the source code of a webpage but when I do that, the UI freezes. I even used SwingWorker but it didn't work. Here is a SSCCE:

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class SSCCE extends JPanel {

    public SSCCE() {
        setPreferredSize(new Dimension(200, 50));
        JFrame frame = new JFrame();
        frame.add(this);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        JButton action = new JButton("Action");
        action.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SwingWorker<String, Void> worker = new SwingWorker<String, Void>(){
                    protected String doInBackground() {
                        try {
                            URL url = new URL("http://stackoverflow.com/");
                            InputStream is = url.openStream();
                            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                            StringBuilder sb = new StringBuilder();
                            String line;
                            while ((line = br.readLine()) != null) {
                                sb.append(line);
                            }
                            if (is != null) is.close();
                            String source = sb.toString();
                            return source;
                        } catch (IOException e) {
                            return null;
                        }
                    }
                };
                worker.execute();
                try {
                    System.out.println(worker.get());
                } catch (HeadlessException | InterruptedException | ExecutionException e1) {
                    e1.printStackTrace();
                }
            }
        });
        add(action);

        JButton nothing = new JButton("Nothing");
        add(nothing);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SSCCE();
            }
        });
    }
}

Is there a better way to get the source code or a way to get it where the UI doesn't freeze? How?

Upvotes: 1

Views: 55

Answers (1)

Your problem is that even though you're using a SwingWorker, you immediately turn around and tell the UI to wait for it (worker.get()). Instead, your worker should use a callback--when its work is done, it should invoke some action, probably on the event thread, that tells the UI to do the next thing.

Upvotes: 3

Related Questions