• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 560
  • Last Modified:

StatusBar In Java....

I'm writing a HTTP web browser in Java with minimum features. Below i have put the code for my statusbar.

I have no clue how to go about it. I require the clock to be ticking and also want to know how to insert the NUM, CAP and other keys status on this. Also want the progress bar to work.

Please Help Urgently!!!

class StatusBar extends JComponent
{
private JProgressBar progress = new JProgressBar();
          JLabel glabelDate, glabelTime;
          Format formatter;
           Date today = new Date();
                     
          public StatusBar()
          {
               super();
     setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
        progress.setBorder(new BevelBorder(BevelBorder.LOWERED));
               add(progress);
                           
               //Add Time to the Statusbar
                formatter = new SimpleDateFormat("HH:mm:ss");
             String time = formatter.format(today);
               glabelTime = new JLabel( time , JLabel.RIGHT);
             glabelTime.setMaximumSize( new Dimension( 40, 20 ));
             glabelTime.setBorder( new BevelBorder( BevelBorder.LOWERED ));
             add(glabelTime);
             
             //glabelTime.paintImmediately(glabelTime.getVisibleRect());                    
             
             //Add Date to the Statusbar
                /*formatter = new SimpleDateFormat("MMM dd, yyyy");
                String date = formatter.format(today);
               glabelDate = new JLabel( date , JLabel.RIGHT);
             glabelDate.setMaximumSize( new Dimension( 40, 20 ));
             glabelDate.setBorder( new BevelBorder( BevelBorder.LOWERED ));
             add(glabelDate);*/

               revalidate();
          }

        public void paint(Graphics g)
          {
               super.paint(g);
          }

          public void setValue(int value)
          {
                progress.setValue(50);
                progress.paintImmediately(progress.getVisibleRect());
           }
     }

Tnx.
0
vks_vicky
Asked:
vks_vicky
  • 2
  • 2
1 Solution
 
Tommy BraasCommented:
For the clock, you would need a Thread that updates it at a certain interval, a second (or 1000ms.) Something like the following might work;

class LabelUpdater
extends Thread
{
   JLabel theLabelToUpdate;
   DateFormat dateFormat;
   long interval;
   Date currentDate;

   public LabelUpdater(JLabel theLabel, DateFormat dateFormat, long interval)
   {
      theLabelToUpdate = theLabel;
      this.dateFormat = dateFormat;
      this.interval = interval;
      currentDate = new Date(System.currentTimeMillis());
   }

   public void run()
   {
      try
      {
         Thread.sleep(interval);
         theLabelToUpdate.setText(dateFormat.format(System.currentTimeMillis()));
      }
      catch(InterruptedException ie)
      {}
   }
}


// insert this in your code where it makes sense
Thread dateTimeUpdater = new LabelUpdater(theLabelToBeUpdated, theDateTimeFormatToUse, thRightInterval);
dateTimeUpdater.start();

As far as the key stuff goes, do something like the following;

yourFrame.addKeyListener(new KeyAdapter()
{
   public void keyPressed(KeyEvent e)
   {
      switch( e.getKeyCode() )
      {
         case KeyEvent.VK_CAPS_LOCK: theObjectRepresentingCapsLock.setStatus(true); break;
         case ...do the same thing for the other ones you want to keep track of
      }
   }
}

You can remove the code for the paint() method, as it adds nothing to your code. It will still execute probably.
0
 
vks_vickyAuthor Commented:
When i try to compile, i get this error...

Browser.java:678: cannot resolve symbol
symbol  : method format (long)
location: class java.text.DateFormat
theLabelToUpdate.setText
(dateFormat.format(System.currentTimeMillis()));
           ^error
0
 
Tommy BraasCommented:
Sorry, my bad! Change the run method to;

 public void run()
  {
     try
     {
        Thread.sleep(interval);
        theLabelToUpdate.setText(dateFormat.format(new Date(System.currentTimeMillis())));
     }
     catch(InterruptedException ie)
     {}
  }
0
 
vks_vickyAuthor Commented:
Tnx. this works fine now....
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now