Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: how could I output to a text area the output of a method

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default how could I output to a text area the output of a method

    guys, I have a GUI that scan a range of ip address when press "scan" button; however, I'm having trouble displaying the result in the textArea box. I'm using a threads for my execution. here's the action performed when press the scan button. How could I get the output of the scan and put it into a textArea.setText(...);

    IPrange netRange = new IPrange();
    ArrayList<String> r = netRange.getRange();
     
    public void StartIPscan() {
            String ipFrom = ipTextFrom.getText();
            String ipTo = ipTextTo.getText();
            netRange.setFromip(ipFrom);
            netRange.setToip(ipTo);
            netRange.StrToIP();
            netRange.calcNetwork();
            /**
             * ping sweep network
             */
            Runnable scan = new IPscanner(r);
            Thread t = new Thread(scan);
            t.start();
     
        }

    here's the IPrange class that implements Runnable interface


    import java.io.*;
    import java.net.*;
    import java.util.*;
    public class IPscanner implements Runnable{
        private String ipadd = "";
        ArrayList<String> block;
        public IPscanner(ArrayList r)
        {
            block = r;
        }
        public void run(){
                boolean ping = false;
                //ipadd = block.size()+"";
                ipadd = "Scanning network "+"\""+block.get(0)+"...\""+"\n";
     
                for(int i = 0; i < block.size(); i++)
                {
                    try
                    {
                        int timeout = 1000;
                        InetAddress address = InetAddress.getByName(block.get(i));
                        //scanProgressBar.setValue(i);//updating progress bar
                        ping = address.isReachable(timeout);
                        if(ping == true)
                        {
                             ipadd += block.get(i)+" ---> is alive!"+"\n";
                        }
                        else
                        {
                            ipadd += block.get(i)+"\n";
                        }
                    }
                    catch(IOException e)
                    {
                        System.out.println(e);
                    }
               }
     
            }
        public void setIPadd(String s)
        {
            ipadd = s;
        }
        public String getIPadd()
        {
            return ipadd;
        }
    }


  2. #2
    Junior Member ShadeDarkan's Avatar
    Join Date
    Jul 2012
    Location
    Houston, TX
    Posts
    13
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: how could I output to a text area the output of a method

    This might not help, but I'm not seeing anywhere where you are calling IPScanner's getIPadd() function. I see that you are putting all the IP addresses into the the IPadd String variable, but I'm not seeing anywhere where you are accessing it. You could make the textArea public and return each value in the IPScanner For loop straight to the textArea.
    Other than that, it sounds like you'll have to keep a reference list of each run job and refer to that to access the member functions so you can pull the ipadd String for each one.

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: how could I output to a text area the output of a method

    Quote Originally Posted by ShadeDarkan View Post
    it sounds like you'll have to keep a reference list of each run job and refer to that to access the member functions so you can pull the ipadd String for each one.
    how could I do the last thing you mentioned?
    Last edited by mia_tech; July 8th, 2012 at 06:24 PM.

  4. #4
    Junior Member ShadeDarkan's Avatar
    Join Date
    Jul 2012
    Location
    Houston, TX
    Posts
    13
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: how could I output to a text area the output of a method

    I've never done something like that before. Theoretically, you would need to keep something like an object list or linked list that would hold a name or pointer to each thread. I'm not too terribly good with Java yet, but if the thread is supposed to terminate after executing, it doesn't look like it does, then your list would only need a name entry and a pointer to the thread in memory.
    So, say you click the Scan button, the action associated creates a new instance of your IPScanner, throws it into its own thread, runs it, then throws, say, a date/time entry into the list for a name entry and a pointer to that particular thread. Then, you can iterate through your list by date/time entries and use the pointer associated with it to access the thread's getIPadd() function to see what IPs were scanned.
    That is just how I understand the process to work. Copeg or someone else with more knowledge can verify that or expand on it.
    Sorry I couldn't be more helpful.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how could I output to a text area the output of a method

    There are quite possibly dozens of ways to accomplish your requirement. You say text area, do you mean JTextArea? One of the easiest to implement would be to pass a reference of the 'textarea' to the class in the constructor, then call the appropriate method when complete (note that usually you need to be careful of Swing and the single threaded rule - in this case the setText method is documented as being thread safe, but check the API docs to be sure). An alternative is to use a SwingWorker and its methods that can be used to notify on the EDT when the work is completed. As an aside, if not already you should also notify the user of progress, say through a JProgressBar, and I do wonder why you are using a JTextArea - seems like a JTable might be appropriate.

  6. #6
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: how could I output to a text area the output of a method

    Quote Originally Posted by copeg View Post
    There are quite possibly dozens of ways to accomplish your requirement. You say text area, do you mean JTextArea? One of the easiest to implement would be to pass a reference of the 'textarea' to the class in the constructor, then call the appropriate method when complete (note that usually you need to be careful of Swing and the single threaded rule - in this case the setText method is documented as being thread safe, but check the API docs to be sure). An alternative is to use a SwingWorker and its methods that can be used to notify on the EDT when the work is completed. As an aside, if not already you should also notify the user of progress, say through a JProgressBar, and I do wonder why you are using a JTextArea - seems like a JTable might be appropriate.
    I tried passing the JTextArea in the constructor to IPScanner class, and use setText method from there, but when I do a second scan, it outputs the results from the previous scan plus the new scan, so I'm getting the same results as before... am I missing something here?

    Thanks

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: how could I output to a text area the output of a method

    Quote Originally Posted by mia_tech View Post
    I tried passing the JTextArea in the constructor to IPScanner class, and use setText method from there, but when I do a second scan, it outputs the results from the previous scan plus the new scan, so I'm getting the same results as before... am I missing something here?
    It's hard to tell with just code snippets. I think that you're going to need to do some debugging on your code so you can find your error.

Similar Threads

  1. Text output in GUI
    By andreas81 in forum Java Theory & Questions
    Replies: 4
    Last Post: June 26th, 2013, 09:36 AM
  2. Writing to output text file
    By gatorsgirl in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2012, 08:17 PM
  3. DIRECTING TEXT OUTPUT AT THE LOCAL MACHINE
    By jai in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: December 20th, 2011, 12:13 AM
  4. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  5. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM