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 JProgressBar update problems HELP!!!

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 don't think you're doing anything wrong. My experience with swing has been that screen updates are low priority - they occur when the program has nothing else to do.

I've solved this in the past by putting the progress bar in a separate thread.

Also, if you're not using the swing that comes with JDK1.4, you should switch to it. Some screen painting bugs (on the Win95 platform, at least) have finally been fixed.

Hope this helps.
 
You need to use a thread for the process that the
progress bar is tracking otherwise it will hog the
cpu and not allow a screen update.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top