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(...);
Code :
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
Code :
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;
}
}
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.
Re: how could I output to a text area the output of a method
Quote:
Originally Posted by
ShadeDarkan
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?
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.
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.
Re: how could I output to a text area the output of a method
Quote:
Originally Posted by
copeg
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
Re: how could I output to a text area the output of a method
Quote:
Originally Posted by
mia_tech
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.