Developing Status bar component in java Swing




Java does not have a built-in StatusBar class in Swing. You need to create your own status bar.The following program is developed for displaying Date and time in Status bar ..This program helps you implement dynamic Status bar related application .

Java Program

// Comment


package up2java.com.code;

/**
 *
 * @author www.up2java.com
 */
   
import java.awt.event.*; 
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;  
public class ClockLabel extends JLabel implements ActionListener {
 String date1;
 StringBuffer css;
  public ClockLabel() { 
       super("" + new SimpleDateFormat("E dd-MMM-yyyy    hh:mm:ss a").format(new Date()));
       Timer t = new Timer(1000,this);
       t.start(); 
         }  
  public void actionPerformed(ActionEvent ae) { 
         setText("" +new SimpleDateFormat("E dd-MMM-yyyy    hh:mm:ss a ").format(new Date()));  
        } 
} 
This program act as a timer to display the date and time with specific time delay .Due to this use will be able to see the date time in the status bar..
// Comment



package up2java.com.code;

/**
 *
 * @author up2java.com
 */
public class StatusBarSample extends javax.swing.JFrame {
ClockLabel clock = new ClockLabel();
    /**
     * Creates new form StatusBarSample
     */
    public StatusBarSample() {
        initComponents();
        jToolBar1.add(clock);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // 
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("UP2JAVA.COM");

        jToolBar1.setRollover(true);
        jToolBar1.setName("jt");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 275, Short.MAX_VALUE)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// 

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StatusBarSample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StatusBarSample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StatusBarSample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StatusBarSample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new StatusBarSample().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration
}

This java program is responsible for frame which contain jToolBar as a status bar

 ClockLabel clock = new ClockLabel(); 
 Above line creates object clock which contains timer to display date and time.

jToolBar1.add(clock);
Here cloak object has been added to the jtoolbar component by the jToolbar now act as a Status bar.For running this program we have used Net beans..

                                                                            Output

raja P Web Developer

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...