how i attach a calendar near the box of date of birth in application form of java
Hello Friend,
Try the following code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; class DatePicker{ int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH); int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);; JLabel l = new JLabel("",JLabel.CENTER); String day = ""; JDialog d; JButton[] button = new JButton[49]; public DatePicker(JFrame parent){ d = new JDialog(); d.setModal(true); String[] header = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}; JPanel p1 = new JPanel(new GridLayout(7,7)); p1.setPreferredSize(new Dimension(430,120)); for(int x = 0; x < button.length; x++){ final int selection = x; button[x] = new JButton(); button[x].setFocusPainted(false); button[x].setBackground(Color.white); if(x>6)button[x].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ day = button[selection].getActionCommand(); d.dispose(); }}); if(x < 7){ button[x].setText(header[x]); button[x].setForeground(Color.red); } p1.add(button[x]); } JPanel p2 = new JPanel(new GridLayout(1,3)); JButton previous = new JButton("<< Previous"); previous.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ month--; displayDate();}}); p2.add(previous); p2.add(l); JButton next = new JButton("Next >>"); next.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ month++; displayDate(); } }); p2.add(next); d.add(p1,BorderLayout.CENTER); d.add(p2,BorderLayout.SOUTH); d.pack(); d.setLocationRelativeTo(parent); displayDate(); d.setVisible(true); } public void displayDate(){ for(int x = 7; x < button.length; x++) button[x].setText(""); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMMM yyyy"); java.util.Calendar cal = java.util.Calendar.getInstance(); cal.set(year,month,1); int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK); int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH); for(int x = 6+dayOfWeek,day = 1; day <= daysInMonth; x++,day++) button[x].setText(""+day); l.setText(sdf.format(cal.getTime())); d.setTitle("Date Picker"); } public String setPickedDate() { if(day.equals("")) return day; java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy"); java.util.Calendar cal = java.util.Calendar.getInstance(); cal.set(year,month,Integer.parseInt(day)); return sdf.format(cal.getTime()); } } class Picker{ public static void main(String[] args){ JLabel label = new JLabel("Selected Date:"); final JTextField text = new JTextField(20); JButton b = new JButton("popup"); JPanel p = new JPanel(); p.add(label); p.add(text); p.add(b); final JFrame f = new JFrame(); f.getContentPane().add(p); f.pack(); f.setVisible(true); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ text.setText(new DatePicker(f).setPickedDate()); } }); } }
Thanks
you can even try this .......But you need to have a jar file to execute this...if you need them i will forward it to your mail
import com.qt.datapicker.DatePicker; import java.util.Calendar; import java.util.Locale; import java.util.Observable; import java.util.Observer; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.text.JTextComponent;
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
/** * * @author GB15 */ public class ob extends javax.swing.JFrame {
static Locale locale; ObservingTextField textField1; /** * Creates new form ob */ public ob() { initComponents(); textField1 = new ObservingTextField(); textField1.setColumns(1); textField1.setText(""); textField1.setToolTipText("This is a text field that implments Observer interface."); textField1.setFont(new java.awt.Font("Agency FB", 1, 18)); // NOI18N textField1.setForeground(new java.awt.Color(255, 0, 0)); textField1.setBounds(140, 30, 140, 50); this.add(textField1); } /** * 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") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setMinimumSize(new java.awt.Dimension(500, 460)); getContentPane().setLayout(null); jLabel1.setText("OPENING BALANCE "); getContentPane().add(jLabel1); jLabel1.setBounds(10, 40, 130, 30); jButton1.setText("SELECT DATE"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); getContentPane().add(jButton1); jButton1.setBounds(310, 30, 130, 50); jButton2.setText("OK"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); getContentPane().add(jButton2); jButton2.setBounds(90, 220, 80, 30); jButton3.setText("EXIT"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); getContentPane().add(jButton3); jButton3.setBounds(240, 220, 80, 30); pack(); }// </editor-fold> private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: new ob1(); //this.dispose(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: DatePicker dp; dp = new DatePicker(textField1, locale); // previously selected date java.util.Date selectedDate = dp.parseDate(textField1.getText()); dp.setSelectedDate(selectedDate); dp.start(textField1); String s = textField1.getText(); System.out.println(s); } /** * @param args the command line arguments */ public static void main(String args[]) throws ClassNotFoundException, InstantiationException{ /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* 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(ob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ob.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ String lang = null; if (args.length > 0) { lang = args[0]; } locale = getLocale(lang); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ob().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; // End of variables declaration private static Locale getLocale(String loc) { if (loc != null && loc.length() > 0) { return new Locale(loc); } else { return Locale.US; } }
class ObservingTextField extends JTextField implements Observer {
public void update(Observable o, Object arg) { Calendar calendar = (Calendar) arg; com.qt.datapicker.DatePicker dp = (com.qt.datapicker.DatePicker) o; System.out.println("picked=" + dp.formatDate(new java.util.Date())); setText(dp.formatDate(calendar)); // s=textField.getText(); //System.out.println(s); }
}
}