5 Replies - 1144 Views - Last Post: 01 April 2011 - 07:40 AM Rate Topic: -----

#1 jasperFernandes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-March 11

using a jTextfield to receive the user input

Posted 30 March 2011 - 09:00 PM

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.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")
public class SimpleWebCrawler extends JFrame {
	
	
	static JTextArea _resultArea = new JTextArea(100, 100);
	JScrollPane scrollingArea = new JScrollPane(_resultArea);
	private final static String newline = "\n";
	JTextField yourInputField = new JTextField(16);
	

	
	

	
    public SimpleWebCrawler() throws MalformedURLException {
    	

    	_resultArea.setEditable(false);
    	System.out.println("Please enter the website  :");
		Scanner scan2 = new Scanner(System.in);
		String word2 = scan2.nextLine();

		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();
		}

		System.out.println("\n");
		System.out.println("\n");
		System.out.println("\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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        

        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 {
		
		
       
        JFrame win = new SimpleWebCrawler();
        win.setVisible(true);
        

        
    }
}

	





This is my code to extract links from a website. The output of this program is transferred to a JTextArea.
What i wanted to do now is,

I would like to create a JTextField to receive input URL from the user. Before this, the URL is typed inside the IDE console to run the program. However, i would like to type the URL in a JTextField and initiate the program.

The JTextField will transfer input from the user to this code :

URL my_url = new URL("http://" + word2 + "/");

String url = "http://" + word2 + "/";


Is it possible ?

This post has been edited by macosxnerd101: 30 March 2011 - 09:04 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: using a jTextfield to receive the user input

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9039
  • View blog
  • Posts: 33,531
  • Joined: 27-December 08

Re: using a jTextfield to receive the user input

Posted 30 March 2011 - 09:05 PM

It's definitely possible. Have a look at the documentation.
Was This Post Helpful? 1
  • +
  • -

#3 jasperFernandes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-March 11

Re: using a jTextfield to receive the user input

Posted 31 March 2011 - 08:22 AM

hi, I am not experienced in GUI building. Can you advice me what steps i should use to develop a JTextField in order to receive input from the user ? Maybe some advice is sufficient judging by my code. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9039
  • View blog
  • Posts: 33,531
  • Joined: 27-December 08

Re: using a jTextfield to receive the user input

Posted 31 March 2011 - 08:28 AM

Just:
-Instantiate the JTextField
-Add it to the JPanel that is added to the JFrame
-When you want to get the text from the JTextField, use the JTextField getText() method which returns a String
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1986
  • View blog
  • Posts: 4,831
  • Joined: 10-September 10

Re: using a jTextfield to receive the user input

Posted 31 March 2011 - 03:02 PM

You'll also need to know when the user is ready to submit the text that has been typed into the text field. That could be done with a JButton that could be added in a manner similar to that described by MacOSXNerd for adding the text field.

If you need more examples or tutorials on the use of Swing, the Swing tutorials here in the Java section are a good place to start.
Was This Post Helpful? 1
  • +
  • -

#6 jasperFernandes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-March 11

Re: using a jTextfield to receive the user input

Posted 01 April 2011 - 07:40 AM

View Postmacosxnerd101, on 31 March 2011 - 08:28 AM, said:

Just:
-Instantiate the JTextField
-Add it to the JPanel that is added to the JFrame
-When you want to get the text from the JTextField, use the JTextField getText() method which returns a String


Sir, would you mind verify what i have done is correct?

JTextField yourInputField = new JTextField(20);


content.add(yourInputField);



Is it correct so far on what you advice me to do ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1