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

Java Program to Import data from textfile to mysql Database

Mysql Posses the functionality  to import data from txt,csv files..Following program helps to import sample text data in to table in database.

Sample data need to be imported


temp.txt file 
--------------


"1 string"      100

"2 string"      102
"3 string"      104
"4 string"      106
  

Table Structure



CREATE TABLE testtable

(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

text varchar(45) NOT NULL,
price integer not null);

Java Program

 
// Comment

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class automateImport
{
    public static void main(String[] args) 
    {
        DBase db = new DBase();
        Connection conn = db.connect(
    "jdbc:mysql://localhost:3306/test","root","caspian");
        db.importData(conn,args[0]);
    }

}

class DBase
{
    public DBase()
    {
    }

    public Connection connect(String db_connect_str, 
  String db_userid, String db_password)
    {
        Connection conn;
        try 
        {
            Class.forName(  
    "com.mysql.jdbc.Driver").newInstance();

            conn = DriverManager.getConnection(db_connect_str, 
    db_userid, db_password);
        
        }
        catch(Exception e)
        {
            e.printStackTrace();
            conn = null;
        }

        return conn;    
    }
    
    public void importData(Connection conn,String filename)
    {
        Statement stmt;
        String query;

        try
        {
            stmt = conn.createStatement(
    ResultSet.TYPE_SCROLL_SENSITIVE,
    ResultSet.CONCUR_UPDATABLE);

            query = "LOAD DATA INFILE '"+filename+
    "' INTO TABLE testtable (text,price);";

            stmt.executeUpdate(query);
                
        }
        catch(Exception e)
        {
            e.printStackTrace();
            stmt = null;
        }
    }
}; 


Java Program to export data from msql database to CSV file

Due to this program its possible to export data from mysql database to cvs file using java code...
// Comment


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class automateExport {
    public static void main(String[] args) {
        DBase db = new DBase();
        Connection conn = db.connect(
                "jdbc:mysql://localhost:3306/test","root","caspian");
        
        if (args.length != 1) {
            System.out.println(
                    "Usage: java automateExport [outputfile path] ");
            return;
        }
        db.exportData(conn,args[0]);
    }
    
}

class DBase {
    public DBase() {
    }
    
    public Connection connect(String db_connect_str, 
            String db_userid, String db_password) {
        Connection conn;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(db_connect_str,
                    db_userid, db_password);
            
        } catch(Exception e) {
            e.printStackTrace();
            conn = null;
        }
        return conn;
    }
    
    public void exportData(Connection conn,String filename) {
        Statement stmt;
        String query;
        try {
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            
            //For comma separated file
            query = "SELECT id,text,price into OUTFILE  '"+filename+
                    "' FIELDS TERMINATED BY ',' FROM testtable t";
            stmt.executeQuery(query);
            
        } catch(Exception e) {
            e.printStackTrace();
            stmt = null;
        }
    }
};


Java Programming IDE App for Android

 

           
Now a days Android smart phones has been  playing a vital roles in market.. Lot of user thinks of how to compile and execute Java program…. Sand is an integrated development environment(IDE) for Java in Android platform .Due to this user shall be able to compile and execute the java programs.. Sand has a very powerful Java editor inside, keywords highlighting for Java, showing line number, undo/redo feature, automatically close are all supported. Sand has a complete Java compiler, Java projects can be built by Sand. Sand can run Java programs, output and input are both supported by Console of Sand
In a word, Sand turns your Android device into a real development tool.



Features of Java programming Ide in android.

A powerful Java editor. Keywords highlighting, showing line number, undo/redo feature, automatically close, automatically indentation are all supported

A complete Java compiler. Support from Java 1.3 to Java 1.6.(Android doesn't support Java 1.7)

Bluetooth keyboard supported

A Console which is in charge of Input from users and Output from compiler and Java programs

Import external JARs into Java project

Import your Favorite RT.jar


                          
You can download rt.jar(1.6.0_24) from the project web page:
download rt.jar


Cons

The memory Sand can use is limited. When you compile and run some very large Java project, Sand may become a little bit slow. It depends on the hardware of your device. 
 
Related Posts Plugin for WordPress, Blogger...