Friday, December 6, 2013

Java Date chooser panel tutorial using microba library

1) Download the "microba" library from here ,it is free for personal as well as commercial use, you can verify from its website  link.
2) Create new java project and class.I made project  with the name"Client" and class name "Class1.java".

3) Extract the "microba-0.4.4.3-full.zip" folder and add the "microba-0.4.4.3.jar" file in the "project properties">"Libraries and Classpath ".

4) Open "Calss1.java" and add following imports.

import com.michaelbaranov.microba.calendar.DatePicker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.util.Date;

5) Now  a new datepicker can be created and added to a new frame using code:

 JFrame jframe = new JFrame("new Frame");
JPanel jpanel = new JPanel();
final DatePicker  datepicker = new DatePicker(new Date());
jpanel.add(datepicker);
jframe.add(jpanel);

6) Action can be assigned to DatePicker (e.g print the selected date).

datepicker.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(datepicker.getDate());//print the current date
                    
                }
            });
7) DatePicker can be enable and disabled like:

       final JCheckBox jchkbox = new JCheckBox("Enable/Disable");
       jchkbox.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e)  {
                    if (jchkbox.isSelected())
                    {datepicker.setEnabled(false);
                        try {
                            datepicker.setDate(new Date());
                        } catch (PropertyVetoException f) {
                        }
                    }else {
                            datepicker.setEnabled(true);
                        }
                    
                }
            });
                
       jpanel.add(jchkbox);
        jframe.add(jpanel);

9) Don't forget to write following lines in the and to display frame.

        jframe.setSize(200,200);
        jframe.setVisible(true);

Note: Complete code can be downloaded from here.


Tuesday, November 26, 2013