Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 5th, 2005, 1:07 PM   #1
Krista
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 Krista is on a distinguished road
converting from javax.swing.JTextField to java.lang.String

I am writing a program which takes user input from a JTextField and I need to use that field when calling a method. I get an error similar to the following.
required: java.lang.String
found: javax.swing.JTextField

My question is: how do I convert that JTextField into a string so I can use it in my method call?

Sorry that I don't have the actual code or error, but I am at work and don't have it with me. If you need further information I can post it once I get home.

Thanks.
Krista
Krista is offline   Reply With Quote
Old Dec 5th, 2005, 1:26 PM   #2
medamaybe
Newbie
 
Join Date: May 2005
Location: the netherlands
Posts: 11
Rep Power: 0 medamaybe is on a distinguished road
String theText = textField.getText ();

is that what you mean?
medamaybe is offline   Reply With Quote
Old Dec 5th, 2005, 1:28 PM   #3
Krista
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 Krista is on a distinguished road
I will give it a try and see what happens.

Thanks.
Krista is offline   Reply With Quote
Old Dec 5th, 2005, 4:36 PM   #4
Krista
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 Krista is on a distinguished road
method won't recognize variable passed to it

I think that last snippet of code may have done something, as I am getting no errors along those lines.
Everything has compiled without any errors, but when I run and type in a url, it bypasses the method and goes to the catch block. It seems like I am passing nothing to the method.
Can someone have a look and see if I am missing something.

Assign3 class
import java.awt.*;
import java.io.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;


public class Assign3 extends JFrame
{
	private JButton scrape;
	private JTextField site;
	private String url;
	
	public Assign3()
	{
		super("Scrape This Site");
		setLayout(new FlowLayout());
		
		site = new JTextField(20);
		add(site);
		
		scrape = new JButton("Scrape Site");
		add(scrape);
		
			
		ButtonHandler handler = new ButtonHandler();
		scrape.addActionListener(handler);
		
	}
		
	
	private class ButtonHandler implements ActionListener
	{
						
		String string = site.getText();
		//System.out.println(string);
		
		public void actionPerformed( ActionEvent event )
		
		{			
			Scrape scrapeSite = new Scrape();
			scrapeSite.getData(string);
		}
	}
		
	
}

Assign3Test class
import javax.swing.JFrame;
public class Assign3Test

{
	public static void main(String args[])
	{
	
		Assign3 assign3 = new Assign3();
		assign3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		assign3.setSize(275,110);
		assign3.setVisible(true);
	}
}

Scrape class(contains the method to scrape the data and the catch block)
import java.awt.*;
import java.io.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.net.*;
import java.util.Formatter;

public class Scrape
{	
	private Formatter file;
	private String site;
	
	public void getData(String url)
	{
		String site = url;
				
		try
		{
			file = new Formatter("scrapedData.txt");
			String data;
			BufferedReader reader;
			BufferedWriter writer;
				
			
			Socket socket = new Socket(site, 80);
			
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			
			writer.write("GET / HTTP/1.0\n\n");
			writer.flush();
			
			while((data = reader.readLine()) != null)
			{
				file.format("%s", data);
			}
			
		  }
			
		catch (IOException e)
		{
			System.out.println("There was an IOException Error!");
		
		}
	}
}

Last edited by Krista; Dec 5th, 2005 at 4:47 PM. Reason: change of topic
Krista is offline   Reply With Quote
Old Dec 5th, 2005, 4:58 PM   #5
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
What do you see when you print out the actual exception message?
Polyphemus_ is offline   Reply With Quote
Old Dec 5th, 2005, 5:08 PM   #6
Krista
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 Krista is on a distinguished road
When I run the code, the GUI pops up, I am able to type in the text field and press the button. It takes a moment and then the message 'There was an IOException' comes up...then press and key to continue.
The message is what I have put in my catch block, so it seems as though it's not taking the variable into consideration...or perhaps there's no variable being sent. I don't know.
Krista is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC