KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > javax > power > monitor > PowerMonitor

javax.power.monitor
Class PowerMonitor

java.lang.Object
  |
  +--javax.power.monitor.PowerMonitor

public void addPowerMonitorListener(PowerMonitorListener listener)
See Also:
PowerWarningType, PowerMonitorListener.powerWarning(int, javax.power.monitor.PowerWarningType)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void addPowerMonitorListener(PowerMonitorListener listener,
                                    PowerWarningType warningType)
See Also:
PowerMonitorListener.powerWarning(int, javax.power.monitor.PowerWarningType)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getBatteryLevel()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getEstimatedSecondsRemaining()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1393]Battery Check
By Anonymous on 2005/04/14 12:15:11  Rate
Is anybody able to get it working? 
 This program always shows the same, useless values: 
  
  
 //  >  >  >  >  >  >  >  >  >  >  jbattery.JAVA 
  
  
  
 import java.awt.*;  
 import java.awt.event.*;  
 import java.util.*;  
 import java.io.*;  
  
  
  import javax.power.monitor.*;  
  
  
 public class jbattery extends Frame implements ActionListener  {  
    
     private Label lab1,lab2,lab3;  
     private Button exitButton, goButton;  
     private PowerMonitor pMon = null;     
      
     public jbattery (  )  {  
            
           super ( "Battery Check" ) ;  
            
           int remaining, level;  
            
           this.addWindowListener (   
              new WindowAdapter (  )  {   
                    public void windowClosing ( WindowEvent e )  {   
                       System.exit ( 0 ) ;  
                     }   
                  }   
               ) ;  
            
           initGui (  ) ;  
            
           pMon = PowerMonitor.getInstance (  ) ;  
            
                
           setSize ( 200,280 ) ;  
           updateInfo (  ) ;  
            
           setVisible ( true ) ;  
      }   
            
      
     private void initGui (  )  {   
      
              setLayout ( null ) ;  
              lab1 = new Label ( "%" ) ;  
              lab2=new Label ( "secs" ) ;  
              lab3=new Label ( "-power-" ) ; 
              exitButton = new Button ( "Exit" ) ;  
              goButton = new Button ( "Update" ) ;  
      
              //******** Set controls positions.  
              lab1.setBounds ( 10 , 30 , 180, 20 ) ;  
              lab2.setBounds ( 10 , 50 , 180, 20 ) ;  
              lab3.setBounds ( 10 , 70 , 180, 20 ) ;  
              exitButton.setBounds ( 70,250,50,20 ) ;  
              exitButton.addActionListener ( this ) ;  
              goButton.setBounds ( 145,70,50,20 ) ;  
              goButton.addActionListener ( this ) ;  
      
              //******** Add control to frame.  
              add ( lab1 ) ;  
              add ( lab2 ) ;  
              add ( lab3 ) ;  
              add ( exitButton ) ;  
              add ( goButton ) ;  
     }   
      
    // Re-fattorizzazione e miglioramenti  
    private void updateInfo (  )  {   
               
                int remaining,level; 
                boolean ch;  
                int hour, min, sec;  
                  
                if  ( pMon!=null )  {   
              remaining = pMon.getEstimatedSecondsRemaining (  ) ;  
                 level = pMon.getBatteryLevel (  ) ; 
                 ch=pMon.usingExternalPowerSource (  ) ;  
                  
                 hour = remaining /3600;  
                 min =  ( remaining % 3600 )  /60;  
                 sec = remaining - hour*3600 - min*60;  
                  
                 lab1.setText (  remaining+ " - "+hour+" h "+min+" min "+ sec +" sec left." ) ;  
                 // oppure se non funziona la riga sopra sostituiscila con questa sotto  
                 // lab1.setText ( new Integer ( remaining ) .toString (  ) +" estimated time left." ) ;  
                 lab2.setText ( new Integer ( level ) .toString (  ) +"% charge level." ) ;  
                 if  ( ch )   { lab3.setText ( "--- > Charging" ) ; }  
                 else  { lab3.setText ( "Battery" ) ; }  
                  
               } else {   
                 lab1.setText ( "Estimated time left: n/a" ) ;  
                 lab2.setText ( "Charge level: n/a" ) ;  
                 lab3.setText ( "Charging: n/a" ) ; 
               }   
          
     }   
  
  
    public void actionPerformed ( ActionEvent evt )   
     {   
        if  ( evt.getSource (  ) .equals ( goButton )  )  {   
            updateInfo (  ) ;               
         }   
        
        if  ( evt.getSource (  ) .equals ( exitButton )  )   {   
           System.exit ( 0 ) ;  
         }   
     }      
      
      
    public static void main ( String [  ]  args )  {   
           new jbattery (  ) ; 
     }      
  
  
  }  // program end.  
  
  
 


public static PowerMonitor getInstance()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected static PowerMonitor instance
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected PowerMonitor()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void removePowerMonitorListener(PowerMonitorListener listener)
See Also:
PowerWarningType, PowerMonitorListener.powerWarning(int, javax.power.monitor.PowerWarningType)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void removePowerMonitorListener(PowerMonitorListener listener,
                                       PowerWarningType warningType)
See Also:
PowerMonitorListener.powerWarning(int, javax.power.monitor.PowerWarningType)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean usingExternalPowerSource()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags