Hi, i wanted to create a JTextField to receive the user input instead of having the user key in the input through the IDE console. This is my code. Now, my code just runs fine if the user key in the inputs through the console. After running the code, the result i append to text area meaning after the run is complete, a text area will pop out show the results. Now what i wanted to do is i want create a GUI Jtextfield to accept user input rather than having user to key in through the console. This is my code.

import org.jsoup.Jsoup;
import javax.swing.*;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

@SuppressWarnings({ "unused", "serial" })
public class SimpleWebCrawler extends JDialog {

	JTextField yourInputField = new JTextField(20);
	static JTextArea _resultArea = new JTextArea(200, 200);
	JScrollPane scrollingArea = new JScrollPane(_resultArea);
	private final static String newline = "\n";

	public SimpleWebCrawler() throws MalformedURLException {

		System.out.println("Please enter the URL");
		Scanner scanner = new Scanner(System.in);
		String word2 = scanner.nextLine();

		_resultArea.setEditable(false);

		try {
			URL my_url = new URL("http://" + word2 + "/");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					my_url.openStream()));
			String strTemp = "";
			while (null != (strTemp = br.readLine())) {
				_resultArea.append(strTemp + newline);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		_resultArea.append("\n");
		_resultArea.append("\n");
		_resultArea.append("\n");

		String url = "http://" + word2 + "/";
		print("Fetching %s...", url);

		try {
			Document doc = Jsoup.connect(url).get();
			Elements links = doc.select("a[href]");

			System.out.println("\n");

			BufferedWriter bw = new BufferedWriter(new FileWriter(
					"C:\\Users\\user\\fypworkspace\\FYP\\Link\\abc.txt"));
			_resultArea.append("\n");
			for (Element link : links) {
				print("  %s  ", link.attr("abs:href"), trim(link.text(), 35));

				bw.write(link.attr("abs:href"));
				bw.write(System.getProperty("line.separator"));
			}
			bw.flush();
			bw.close();
		} catch (IOException e1) {

		}
		JPanel content = new JPanel();

		content.setLayout(new BorderLayout());
		content.add(scrollingArea, BorderLayout.CENTER);

		this.setContentPane(content);
		this.setTitle("Crawled Links");

		this.pack();

	}

	private static void print(String msg, Object... args) {

		_resultArea.append(String.format(msg, args) + newline);
	}

	private static String trim(String s, int width) {
		if (s.length() > width)
			return s.substring(0, width - 1) + ".";
		else
			return s;
	}

	// .. Get the content pane, set layout, add to center

	public static void main(String[] args) throws IOException {

		JDialog win = new SimpleWebCrawler();
		win.setVisible(true);

	}

}

Can anyone guide me to do the text field from the looks of my code ? Thanks. The user receive input from this line of code.

System.out.println("Please enter the URL");
		Scanner scanner = new Scanner(System.in);
		String word2 = scanner.nextLine();

                URL my_url = new URL("http://" + word2 + "/");
                
		String url = "http://" + word2 + "/";
String stringname = textfield.getText();

JOptionPane.showMessageDialog(null, stringname);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.