Results 1 to 6 of 6
Like Tree2Likes
  • 2 Post By JosAH

Thread: Pinging an IP adress that is taken from a TextField.

  1. #1
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Pinging an IP adress that is taken from a TextField.

    hey.
    im making a program with different sorts of buttons and textfields, to execute commands in the command prompt. (such as ping, ipconfig, and more).

    im kinda stuck on the ping option.

    i have a JTextField where the user has to fill in an IP adress to ping.
    then i save that ip adress in a string and i tried to make a command out of that.
    check out how i did it below:
    Java Code:
    	JButton btnNewButton_7 = new JButton("Ping -->");
    		btnNewButton_7.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				try{
    				    
    					String ipadd = textField_1.getText();
    					Process p = Runtime.getRuntime().exec("cmd /c ping"+ipadd);
    					BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    					String line = reader.readLine();
    					
    					while(line != null){
    						textArea.append(line+"\n");
    						line = reader.readLine();
    					}
    				
    					}catch(IOException e3){}
    			}
    		});
    		btnNewButton_7.setBounds(221, 154, 107, 31);
    		getContentPane().add(btnNewButton_7);
    but when i click the button it doesnt do anything at all.

    i know that i cant put a string on that process, but i don't know any other way i could do this.

    so what i want: user filling in an ip adress in the textfield, then i want to make the command ping the filled in ip adress.

    any solutions?

    Thanks
    Never give up.

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,993
    Rep Power
    5

    Default Re: Pinging an IP adress that is taken from a TextField.

    Check out this epic article:

    When Runtime.exec() won't - JavaWorld

    And you will want to do the actual ping in a separate thread; you don't want the action listener to be on hold while the ping command is doing its thing, it runs on the EDT and so will block the EDT. That is most likely the reason why you don't "see" anything happening.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,518
    Blog Entries
    7
    Rep Power
    20

    Default Re: Pinging an IP adress that is taken from a TextField.

    Quote Originally Posted by MR bruto View Post
    Java Code:
    	Process p = Runtime.getRuntime().exec("cmd /c ping"+ipadd);
    Also put a space between the 'ping' command and the IP address.

    Jos
    gimbal2 and MR bruto like this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,993
    Rep Power
    5

    Default Re: Pinging an IP adress that is taken from a TextField.

    Doh! Nice catch.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,518
    Blog Entries
    7
    Rep Power
    20

    Default Re: Pinging an IP adress that is taken from a TextField.

    Quote Originally Posted by gimbal2 View Post
    Doh! Nice catch.
    I guess the code will run (sort of) fine as is; except for the output in the JTextArea; it will only be displayed when the ping command has finished and the ActionListener has finished as well, so your hint most certainly shouldn't be ignored.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: Pinging an IP adress that is taken from a TextField.

    Quote Originally Posted by gimbal2 View Post
    Check out this epic article:

    When Runtime.exec() won't - JavaWorld

    And you will want to do the actual ping in a separate thread; you don't want the action listener to be on hold while the ping command is doing its thing, it runs on the EDT and so will block the EDT. That is most likely the reason why you don't "see" anything happening.
    Thanks man! i fixed it!
    Last edited by MR bruto; 07-05-2013 at 11:38 AM.
    Never give up.

Similar Threads

  1. One row help with Locking Java to MAC Adress
    By Darkbound in forum New To Java
    Replies: 1
    Last Post: 04-28-2011, 07:42 PM
  2. Won't set to textfield
    By Steffers in forum New To Java
    Replies: 7
    Last Post: 11-04-2010, 02:32 PM
  3. Get Ip adress?
    By Godsent in forum Networking
    Replies: 2
    Last Post: 07-11-2009, 02:15 PM
  4. TextField Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:21 PM
  5. JSP - getting value from a textfield
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:58 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
  •