Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swing JProgress Bar update problems

Status
Not open for further replies.

clax99

Programmer
Oct 29, 2001
23
0
0
US
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(&quot;Connected on port &quot; + i);
script.append(&quot;Connected to port &quot; + i + '\n');
test.close();
progress.setValue((i/((en-st)+1)) * 100);
}
catch(IOException e){
System.out.println(&quot;No connection on port &quot; + i);
script.append(&quot;Port &quot; + i + &quot; closed&quot; + '\n');
progress.setValue((i/((en-st)+1)) * 100);
}

}

}catch(UnknownHostException e){
script.append(&quot;Unknown host/not resovled&quot;);
}

}


public static void main(String args[]){
Netface n = new Netface();

n.setVisible(true);
}
}
 
I have had good success with progress bars in the past and the only time I had a similar problem was because I underestimated the value being passed to the bar.

You are always multiplying the value by 100 before it is passed to the progress bar. Look at your equation - i/((en-st)+1 - can you guarantee that the outcome will always be < 1?

If I was you, I suggest you set the progress bar to go to 1000, or perhaps multiply by 10 rather than 100 and see what happens.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top