content top

comparator vs comparable

Comparable:
A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances. This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class’s natural ordering, and the class’s compareTo method is referred to as its natural comparison method.

Comparator:

Comparator is an interface which is used for total ordering for some collection of objects. If you don’t rely on natural ordering that is done by class’s by implementing the comparable interface, you can define your custom comparator.

It is also useful when you have a predefined class in some jar library that don’t have implementation of comparable interface or that implementation is not as per your requirement. In that case you can use comparator for ordering.

Will post the example later…

Read More

Java File filter example

FileFilter (javax.swing.filechooser.FileFilter) restricts the files that are going to be shown in JFileChooser UI. Default file chooser shows all the files and directory available in a directory in file chooser dialog. You can customize the file filter by overriding the FileFilter’s public boolean accept(File f) method.

Setting a file filter in file chooser

JFileChooser fileChooser = new JFileChooser();
FileFilter filter = new FileFilter() {
// overriding the accept method to accept the .csv files.
            @Override
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().toLowerCase().endsWith(".csv");;
            }
            @Override
            public String getDescription() {
                return "file type .csv is accepted only.";
            }
 } ;
fileChooser.setFileFilter(filter);
Read More

Copy one file to another in java

The following example reads the text file using java.io.InputStream and store it in byte array as buffer and write the buffer content to output stream java.io.OutputStream.
Example: See the following copy method.

/**
     * Copies src file to dest file.
     * If the dest file does not exist, it is created
     *
     */
    public void copy(File src, File dest) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
Read More

Swing option dialog with custom field Text area – JTextArea

Swing Custom option dialog with custom field Text area JTextArea
See the following example.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class CustomOptionDialog extends JFrame {
    public CustomOptionDialog() {
//      initialize the JFrame instance
        this.setSize(new Dimension(300, 200));
        JButton button = new JButton("Show Dialog");
        this.setLayout(new BorderLayout());
        this.getContentPane().add(button, BorderLayout.CENTER);
        this.setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
//              Create an instance of JTextArea
                JTextArea area = new JTextArea("Text area Field");
                area.setRows(5);
                area.setColumns(20);
//              Create Object array to put message and text area on customized dialog
                Object options[] = new Object[]{"Please enter your multiline text.", area};
                int option = JOptionPane.showOptionDialog(null,
                        options,
                        "Title", JOptionPane.YES_NO_OPTION,
                        JOptionPane.INFORMATION_MESSAGE, null,
                        null, null);
                System.out.println(option);
                if (option == JOptionPane.YES_OPTION) {
                    System.out.println(area.getText());
                }
            }
        });
    }
    public static void main(String[] args) {
        new CustomOptionDialog();
    }
}

output:

Read More

Connection pooling in tomcat in Mysql

1. Copy mysql-connector-java-3.0.9-stable-bin.jar in tomcat’s common\lib directory

2. Copy this code in server .xml before the</host> tag.

<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="XXXXX" password="XXXXX" driverClassName="org.gjt.mm.mysql.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
</Context>

where
DBTest is folder in webapp where all files of the application are kept
Jdbc/TestDB is the jndi name
Javatest is the database name.

3. Enter this code in web.xml

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

4. Use this code in ur file where connection has to be established

Context initContext=new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();
Read More

Vaccine scheduler – Reminder via sms

In current scenario of life, communication plays a very important role in moving life ahead. Emailing is a way to communicate. Internet is not in so common practice in rural areas, So for building strong vaccine scheduler i did modification in notification system of Vaccine Scheduler by integrating sms gateway system with existing email notification system. With this integration doctors will be able to send the notification via sms also.
SMS integration enhances the power of communication in Vaccine Scheduler‘s notification System. Main motive of integrating the sms functionality with vaccine scheduler is to provide a way to send notification to parents of rural areas where internet facility is not common.

Thanks

Read More
Page 1 of 3123
content top