?
Solved

StatusBar In Java....

Posted on 2003-02-28
4
Medium Priority
?
526 Views
Last Modified: 2008-02-26
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
Comment
Question by:vks_vicky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8047014
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
 
LVL 5

Author Comment

by:vks_vicky
ID: 8047605
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
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 300 total points
ID: 8049061
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
 
LVL 5

Author Comment

by:vks_vicky
ID: 8050972
Tnx. this works fine now....
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question