8 Replies - 453 Views - Last Post: 02 April 2011 - 09:11 AM Rate Topic: -----

#1 jasperFernandes  Icon User is offline

  • New D.I.C Head

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

JTextField problem

Posted 02 April 2011 - 06:06 AM

import org.jsoup.Jsoup;


@SuppressWarnings("unused")
public class SimpleWebCrawler extends JFrame {
	
	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 {
    	
    
    	_resultArea.setEditable(false);
    	
		String word2 = yourInputField.getText();

		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);
        content.add(yourInputField);
          
        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);
      
    }
}

	





I am trying to create a JTextField to receive input from the user. I have created an instance of JTextField and added into the JFrame. However, this code is not working. Mind point out my mistakes ? It suppose to work, however i could not find out the problem is. I am out of option and kinda desperate. Thank you for your advice.

JTextField yourInputField = new JTextField(20);
String word2 = yourInputField.getText();
content.add(yourInputField);


Is This A Good Question/Topic? 0
  • +

Replies To: JTextField problem

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1987
  • View blog
  • Posts: 4,840
  • Joined: 10-September 10

Re: JTextField problem

Posted 02 April 2011 - 06:34 AM

What do you mean by "not working?"

If you're wondering why word2 = null, it's because you're reading the contents of a brand new text field, and it'll be empty unless you initialize it to something.
Was This Post Helpful? 0
  • +
  • -

#3 jasperFernandes  Icon User is offline

  • New D.I.C Head

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

Re: JTextField problem

Posted 02 April 2011 - 06:38 AM

View PostGregBrannon, on 02 April 2011 - 06:34 AM, said:

What do you mean by "not working?"

If you're wondering why word2 = null, it's because you're reading the contents of a brand new text field, and it'll be empty unless you initialize it to something.


It does not show the Jtextfield and it instantly prints out IllegalArgumentException. I already created the getText method to get the string, however, it do not work. It straight print out IllegalArgument exception upon running.
Was This Post Helpful? 0
  • +
  • -

#4 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: JTextField problem

Posted 02 April 2011 - 06:43 AM

Hi, I got a solution its not efficient but I think it will work try adding a JButton and an ActionListener to that button and let the user press it when he finish writing what he needs in the text field and in the ActionPerformed use the .getText() method.
JButton jButton = new JButton("Send Text");
jButton.addActionListner(this);


      public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equalsIgnoreCase("Send Text")){
                String s2 = txt.getText();
    
        }
    }


This post has been edited by kngofdrkns: 02 April 2011 - 06:45 AM

Was This Post Helpful? 1
  • +
  • -

#5 jasperFernandes  Icon User is offline

  • New D.I.C Head

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

Re: JTextField problem

Posted 02 April 2011 - 06:58 AM

View Postkngofdrkns, on 02 April 2011 - 06:43 AM, said:

Hi, I got a solution its not efficient but I think it will work try adding a JButton and an ActionListener to that button and let the user press it when he finish writing what he needs in the text field and in the ActionPerformed use the .getText() method.
JButton jButton = new JButton("Send Text");
jButton.addActionListner(this);


      public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equalsIgnoreCase("Send Text")){
                String s2 = txt.getText();
    
        }
    }



Hi, thank you for your reply. In this line of code,
jButton.addActionListner(this);

i got the misplace constructs error and syntax error on token 'this'. Which is an invalid type.
DO i miss anything ?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,555
  • Joined: 27-December 08

Re: JTextField problem

Posted 02 April 2011 - 07:03 AM

Your class doesn't implement ActionListener. You have to pass another ActionListener object, or have your class implement ActionListener.
Was This Post Helpful? 0
  • +
  • -

#7 kngofdrkns  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 82
  • Joined: 21-May 08

Re: JTextField problem

Posted 02 April 2011 - 07:04 AM

sorry my mistake you must add to the class implements ActionListener :surrender: something like this
static class GUIComponent implements ActionListener {
JButton jButton = new JButton("Send Text");
jButton.addActionListner(this);

}
public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equalsIgnoreCase("Send Text")){
            String s2 = txt.getText();

    }
}


This post has been edited by kngofdrkns: 02 April 2011 - 07:07 AM

Was This Post Helpful? 1
  • +
  • -

#8 jasperFernandes  Icon User is offline

  • New D.I.C Head

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

Re: JTextField problem

Posted 02 April 2011 - 07:39 AM

View Postmacosxnerd101, on 02 April 2011 - 07:03 AM, said:

Your class doesn't implement ActionListener. You have to pass another ActionListener object, or have your class implement ActionListener.


hi, where should i put the actionlistener ? It seems that wherever i put the actionlistener, it clashes with the other codes.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,555
  • Joined: 27-December 08

Re: JTextField problem

Posted 02 April 2011 - 09:11 AM

As kngofdrkns demonstrated, you can have your GUI class implement ActionListener. :)

class MyFrame extends JFrame implements ActionListener{
   
     JButton button;

     MyFrame(){
        button = new JButton();

        //since MyFrame implements ActionListener
        //it is-an ActionListener, so you can now pass 
        //this to the addActionListener() method
        button.addActionListener(this);
     }

     public void actionPerformed(ActionEvent e){}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1