![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Newbie
Join Date: May 2005
Location: the netherlands
Posts: 11
Rep Power: 0
![]() |
String theText = textField.getText (); is that what you mean? |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
I will give it a try and see what happens.
Thanks. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
What do you see when you print out the actual exception message?
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|