Results 1 to 9 of 9

Thread: Swing input

  1. #1
    Yuuki is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default Swing input

    Hi can we take input for a URL using swing? supposed im taking input
    Name:"Bobby" in the text box/Text Field
    and i use the getText() method to get the text from the text box...
    and print "You have entered Bobby"

    But when i take a URL as input this doesn't work :(
    Do you know how i can take a URL as input from swing..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Yuuki View Post
    But when i take a URL as input this doesn't work :(
    Do you know how i can take a URL as input from swing..
    It works fine for me. Please show us a small program that demonstrates how it isn't working.

  3. #3
    Yuuki is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    This is a portion of the Code..im new so it might look really lame

    //create the components
    lbl1=new JLabel("Enter the URL :");
    jtf = new JTextField(20);
    lbl2=new JLabel();
    jbtn1=new JButton("Submit");
    jta1=new JTextArea(20,40);
    jsp1=new JScrollPane(jta1);
    jbtn1.addActionListener(this);

    // Add text field to content pane
    contentPane.add(lbl1);
    contentPane.add(jtf);
    contentPane.add(jbtn1);
    contentPane.add(lbl2);
    contentPane.add(jsp1);

    jbtn1.setToolTipText("Hi bianca");

    this.setSize(600, 400);

    // pack();
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    //end of UI design

    String sourceLine;


    // The URL address of the page to open.
    url =jtf.getText().toString() ;

    try{
    URL address = new URL(url);

    // Open the address and create a BufferedReader with the source code.

    pageInput = new InputStreamReader(address.openStream());
    source = new BufferedReader(pageInput);

  4. #4
    Yuuki is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    it doesn't work and it seems as if im passing nothing in the text Field x_x

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think we need to see more. For instance, where are you calling this line?
    Java Code:
    url =jtf.getText().toString() ; // by the way, toString() is not needed as getText returns String

    Is it in the actionPerformed method of an ActionListener?

    Also, please read my signature link on using code tags -- thanks!

    Much luck!

  6. #6
    Yuuki is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    No its not in the actionPerformed method.I tried keeping it there but its still not taking the URL from the textfield.Im calling toString() because i've tried many things but its still not working.Can you please provide some sample code for taking a URL from a textfield and passing that URL to a BufferedReader ?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Yuuki View Post
    No its not in the actionPerformed method.
    My guess is then that you're trying to read the contents of the JTextField when the GUI is starting up before any text has been added by the user. The usual way to get text from these critters is to do it on an event such as a button push (hence the use of an ActionListener) or on pressing enter when within the JTextField (again this uses an ActionListener).


    I tried keeping it there but its still not taking the URL from the textfield.Im calling toString() because i've tried many things but its still not working.Can you please provide some sample code for taking a URL from a textfield and passing that URL to a BufferedReader ?
    All you need to do is get the String and my sample code to do this is no better than what is posted in the Java tutorials. I think that we still need to get a better understanding of what you're doing wrong in order to solve this.

    Again, I suggest you get the JTextField's text from within the ActionListener of a JButton. Try that, and if it still doesn't work, please post your code (but please use code tags).

    Luck!

  8. #8
    Yuuki is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    it worked ^_^

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Input help
    By rice in forum New To Java
    Replies: 6
    Last Post: 09-26-2009, 07:07 PM
  2. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM
  3. Polled keyboard input through swing
    By Prometheus in forum Advanced Java
    Replies: 2
    Last Post: 02-04-2008, 04:05 PM
  4. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •