开发者

Monitoring HDD space... how to show the information in real time?

I have a java application with a label text, and shows the actual remaining hard disk space. I created another class, a "Runnable" one which reads every X seconds how much space left on HDD.

Now my problem: how can I show in the "statatic" label in the JFrame, that information? The closest thing I thought should work, was create a function in the "runneable" class returning the "double" number, and instance that class in the JFrame, but it looks like that JFrame runs just once that line and there's no update in the code.

How can I monitor this? I want to monitor other variables too, but this is the beggining.


Code: To read HDD space

package gwcontrol;
import java.io.File;

public class Analisis implements Runnable{

long delay;
File file = new File("C:\\"); 
float FreeSpace;
float TotalSpace;

public Analisis(long delay){
    this.delay = delay;
    //setDaemon(true);
}

public void run(){

    try {
        while (true) {
        this.FreeSpace = (float) file.getFreeSpace() / 1024 / 1024 / 1024;
        System.out.println("Espacio libre: " + this.FreeSpace);
        Thread.sleep(this.delay);
        }

    } catch (InterruptedException e) {
        //System.out.println("Error" + e);
    }
}

public float getFreeSpace(){
    return this.FreeSpace;
}

public float getTotalSpace(){
    return this.TotalSpace;
}

}

Code: The GUI (not all the code, just the needed IMO)

public class ControlGUI extends javax.swing.JFrame {

/** Creates new form ControlGUI */

public Analisis analisis = new Analisis(1000);

public ControlGUI() {

    try{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch (Exception e){
    System.err.println( e );
    }

    new Thread(analisis).start();

    File file = new File("C:\\"); 

    float FreeSpace = (float) file.getFreeSpace() / 1024 / 1024 / 1024;
    float TotalSpace = (float) file.getTotalSpace() / 1024 / 1024 / 1024;  
    float UsedSpace = TotalSpace - FreeSpace;  

    setTitle("GWControl - A+V - v.1.0");
    initComponents();

    jProgressBar1.setMaximum((int)TotalSpace);
    jProgressBar1.setMinimum(0);
    jProgressBar1.setValue((int)UsedSpace);

    lbl_espacio_libre.setText("HDD: " + analisis.getFreeSpace() 开发者_如何学C+ " Mb libres");
    lbl_porcentaje_espacio.setText(UsedSpace*100/TotalSpace + "% del disco utilizado");

}

Thanks!!


Updated Code:

By reading HERE, I made these changes:

in ControlGUI.java:

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            ControlGUI control = new ControlGUI();
            control.setVisible(true);
        }
    });
}

and Inside the constructor:

this.updateProgress(analisis.getFreeSpace());

[...]

private void updateProgress(final double numero) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          lbl_espacio_libre.setText("HDD: " + numero + " Mb libres");
        }
    });
}

I can't get it working yet =/


you need to create a separate thread which runs in the background (e.g. using a ScheduledExecutorService), periodically gathers the information you need, and then updates the relevant swing components (using SwingUtilities.invokeLater()).

or use a javax.swing.Timer (as Kaj pointed out in the comments), which will probably simplify some of this for you.


Finally!!! thanks to @jtahlborn for the library / function

public class ControlGUI extends javax.swing.JFrame {

pruebas prueba;

public ControlGUI() {

    try{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch (Exception e){
    System.err.println( e );
    }

    final File file = new File("C:\\"); 

    float FreeSpace = (float) file.getFreeSpace() / 1024 / 1024 / 1024;
    float TotalSpace = (float) file.getTotalSpace() / 1024 / 1024 / 1024;  
    float UsedSpace = TotalSpace - FreeSpace;  

    setTitle("GWControl - A+V - v.1.0");

    initComponents();

    jProgressBar1.setMaximum((int)TotalSpace);
    jProgressBar1.setMinimum(0);
    jProgressBar1.setValue((int)UsedSpace);

    prueba = new pruebas(lbl_espacio_libre);
    prueba.start();

    lbl_porcentaje_espacio.setText(UsedSpace*100/TotalSpace + "% del disco utilizado");
}

and

package gwcontrol;
import java.io.File;
import javax.swing.SwingUtilities;

public class pruebas extends Thread implements Runnable {

float FreeSpace;
File file;
javax.swing.JLabel label;

public pruebas(javax.swing.JLabel label){

    this.label = label;
    file = new File("C:\\"); 
}

 public void run() {
    while (true) {

        try {
            this.FreeSpace = (float) file.getFreeSpace() / 1024 / 1024 / 1024;
            System.out.println("Espacio libre: " + this.FreeSpace);
            Thread.sleep(1000);
        } catch (Exception e) {
            System.out.println("Problema: " + e);
        }

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              label.setText("HDD: " + FreeSpace + " Mb libres");
            }
        });
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜