Results 1 to 5 of 5
  1. #1
    pinkette is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Exclamation Reading From A JTextfield - String/Int problem

    Hey guys, im trying to read input from a text box and assign the variables to a socket connection, for example im doing this:

    String ip = jTextField1.getText();
    String port = jTextField2.getText();

    s1 = new Socket(ip, port);

    The IP Address one works, but the port does not - i was thinking maybe thats because it needs an INT value not a STRING, i tried something like:

    String port = jTextField2.getInt();
    or
    INT port = jTextField2.getText();

    But neither will work, does anyone have any ideas? Many thanks!

  2. #2
    mi14chal is offline Member
    Join Date
    Dec 2010
    Posts
    15
    Rep Power
    0

    Default

    Well port must be int look here: Socket (Java 2 Platform SE v1.4.2).
    You must convert String to int for example:
    Java Code:
    int port = Integer.parseInt(jTextField2.getText());

  3. #3
    pinkette is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Your sir are brilliant! I know it had to be converted but wasnt sure on how to do it. I have another problem.

    If i were to have the port text field, but want you to add 4 numbers - and if you dont add any or over 4 etc it would come up with a pop up box, any way i can do that?

  4. #4
    pinkette is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    A pop up box that says, error you have to enter four numbers or something like that.

  5. #5
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi pinkette - there are a couple of ways to do an input validation. A simple way to do it is to add a validation check to the ActionListener so that when a button is clicked, the check is done, and a dialogbox is shown to the user explaining what went wrong. The code would look something like:

    Java Code:
    if Action Event occurs
        check length of port
        if length is invalid
           show error message to user and clear the text field for re-entering
    you can use JOptionPane.showMessageDialog(string) to display message to use.

    Hope that helps,
    Best,
    --user0--

Similar Threads

  1. Problem with JTextField.. please help!!
    By Asvin in forum New To Java
    Replies: 21
    Last Post: 12-18-2010, 03:49 PM
  2. How can I restrict the length of string in JTextField?
    By vicks.very.cool in forum AWT / Swing
    Replies: 4
    Last Post: 07-29-2010, 05:34 PM
  3. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  4. Got problem with JtextField
    By hungleon88 in forum AWT / Swing
    Replies: 4
    Last Post: 12-06-2008, 03:01 PM
  5. Hejp with deleting string from JTextField
    By satin in forum New To Java
    Replies: 9
    Last Post: 11-08-2008, 11:05 PM

Posting Permissions

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