Im working on a small networking Swing app with a progress bar as declared below. the progress bar is initialized from (0,100). theres a certain number of ports to scan every time, so I map the progress through the ports to an int between 0 and 100. I then use progress.setValue() to set the new value of the progress bar. All this occurs in a for loop in the scan() method; the problem is the progress bar never updates, it just becomes 100% at the end of the loop. I call doLayout() and stuff to update the screen, but it doesnt do it. Heres the code below Help...
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.lang.*;
import java.io.*;
class WindowDestroyer extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public class Netface extends JFrame implements ActionListener{
protected JLabel ip,startPort,endPort,space1,space2;
protected JPanel center,realCenter;
protected JTextField ipField,startField,endField;
protected JButton scan;
protected JProgressBar progress;
protected String host;
protected int start,end;
protected JTextArea script;
protected JScrollPane sc;
public Netface(){
script = new JTextArea(25,20000);
sc = new JScrollPane(script);
script.setText(""
doLayout();
progress = new JProgressBar(0,100);
progress.setValue(0);
progress.setStringPainted(true);
center = new JPanel(new GridLayout(3,3));
realCenter = new JPanel();
ip = new JLabel("Host/IP"
startPort = new JLabel("Start port:"
endPort = new JLabel("End port:"
space1 = new JLabel();
space2 = new JLabel();
ipField = new JTextField(15);
startField = new JTextField(7);
endField = new JTextField(7);
scan = new JButton("Scan"
scan.addActionListener(this);
addWindowListener(new WindowDestroyer());
setSize(600,400);
setTitle("KraneScan v 1.0"
getContentPane().setBackground(Color.red);
center.add(ip);center.add(startPort);center.add(endPort);
center.add(ipField);center.add(startField);center.add(endField);
center.add(space1);center.add(scan);center.add(space2);
getContentPane().add(center,"North"
getContentPane().add(progress,"South"
getContentPane().add(sc);
}
protected void grabInput(){
start = Integer.parseInt(startField.getText());
end = Integer.parseInt(endField.getText());
host = ipField.getText();
System.out.println(start + "," + end + "," + host);
scan(host,start,end);
}
protected void paintComponent(Graphics g){
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "Scan"{
grabInput();
System.out.println("Scan pressed"
}
}
public void scan(String host,int st,int en){
String str = host;
Socket test;
try{
InetAddress n = InetAddress.getByName(str);
for(int i = st;i <= en;i++){
try{
test = new Socket(str,i);
System.out.println("Connected on port " + i);
script.append("Connected to port " + i + '\n');
test.close();
progress.setValue((i/((en-st)+1)) * 100);
}
catch(IOException e){
System.out.println("No connection on port " + i);
script.append("Port " + i + " closed" + '\n');
progress.setValue((i/((en-st)+1)) * 100);
}
}
}catch(UnknownHostException e){
script.append("Unknown host/not resovled"
}
}
public static void main(String args[]){
Netface n = new Netface();
n.setVisible(true);
}
}
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.lang.*;
import java.io.*;
class WindowDestroyer extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public class Netface extends JFrame implements ActionListener{
protected JLabel ip,startPort,endPort,space1,space2;
protected JPanel center,realCenter;
protected JTextField ipField,startField,endField;
protected JButton scan;
protected JProgressBar progress;
protected String host;
protected int start,end;
protected JTextArea script;
protected JScrollPane sc;
public Netface(){
script = new JTextArea(25,20000);
sc = new JScrollPane(script);
script.setText(""
doLayout();
progress = new JProgressBar(0,100);
progress.setValue(0);
progress.setStringPainted(true);
center = new JPanel(new GridLayout(3,3));
realCenter = new JPanel();
ip = new JLabel("Host/IP"
startPort = new JLabel("Start port:"
endPort = new JLabel("End port:"
space1 = new JLabel();
space2 = new JLabel();
ipField = new JTextField(15);
startField = new JTextField(7);
endField = new JTextField(7);
scan = new JButton("Scan"
scan.addActionListener(this);
addWindowListener(new WindowDestroyer());
setSize(600,400);
setTitle("KraneScan v 1.0"
getContentPane().setBackground(Color.red);
center.add(ip);center.add(startPort);center.add(endPort);
center.add(ipField);center.add(startField);center.add(endField);
center.add(space1);center.add(scan);center.add(space2);
getContentPane().add(center,"North"
getContentPane().add(progress,"South"
getContentPane().add(sc);
}
protected void grabInput(){
start = Integer.parseInt(startField.getText());
end = Integer.parseInt(endField.getText());
host = ipField.getText();
System.out.println(start + "," + end + "," + host);
scan(host,start,end);
}
protected void paintComponent(Graphics g){
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand() == "Scan"{
grabInput();
System.out.println("Scan pressed"
}
}
public void scan(String host,int st,int en){
String str = host;
Socket test;
try{
InetAddress n = InetAddress.getByName(str);
for(int i = st;i <= en;i++){
try{
test = new Socket(str,i);
System.out.println("Connected on port " + i);
script.append("Connected to port " + i + '\n');
test.close();
progress.setValue((i/((en-st)+1)) * 100);
}
catch(IOException e){
System.out.println("No connection on port " + i);
script.append("Port " + i + " closed" + '\n');
progress.setValue((i/((en-st)+1)) * 100);
}
}
}catch(UnknownHostException e){
script.append("Unknown host/not resovled"
}
}
public static void main(String args[]){
Netface n = new Netface();
n.setVisible(true);
}
}