attach a calendar in java

attach a calendar in java

how i attach a calendar near the box of date of birth in application form of java

View Answers

November 10, 2010 at 2:43 PM

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


November 21, 2012 at 5:30 PM

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);
}

}

}









Related Tutorials/Questions & Answers:
attach a calendar in java
attach a calendar in java  how i attach a calendar near the box of date of birth in application form of java   Hello Friend, Try the following code:ADS_TO_REPLACE_1 import java.awt.*; import java.awt.event.*; import
Java Calendar !?
Java Calendar !?  I need to create a Java Calendar in an Applet. The calendar displays months (jan - dec) in a 3x4 grid ... jan feb march \n april.... so if a user picks the year 2000 show the calendar from that year. So how do i
Advertisements
Java Calendar issue - Date Calendar
Java Calendar issue  Hi I have having weird situation with java Calendar, its driving me crazy. My requirenment is that i got a xml schema element that accepts only Calendar Type Say element 2011-09-25T13:00:00 Say
Java Coding - Date Calendar
Java Coding  Write a program that prompts the user to enter the year and first day of the year and displays the calendar for the year on the console... 1,2005, your program should display the calendar for each month in the year
What is Calendar class in Java?
What is Calendar class in Java?  Hi, What is Calendar class in Java? I want to learn about this class to make use of it in Java programs. Thanks   Hi, java.util.Calendar is an abstract class in Java and its static
class Calendar - Java Beginners
print a calendar for any month starting Junuary 1,1500.Note that the day... Calendar,include the following operations: a.Determine the first day of the month for which the calendar will be printed.Call this operation firstDayOfMonth
Need an Example of calendar event in java
Need an Example of calendar event in java  can somebody give me an example of calendar event of java
How to attach file to HP Quality Center [QC] using java
How to attach file to HP Quality Center [QC] using java  Hello All... java In QC we have <BR> Test Lab<BR> |_Test Set Instcance<... Run Instance<BR> So i wish to attach files with the <B>Test Run
file attach
file attach  how pick up the file from database and attach it to the mail in java   Hi, Here is the code to read image (file) from...(); } After reading the image you can save into file or attach as attachment in your
How to create and Attach MS Access Reports and Crystal Reorts in Java. - Java Beginners
How to create and Attach MS Access Reports and Crystal Reorts in Java.  How to create and Attach MS Access Reports and Crystal Reorts in Java WITH MS Access 2007 Database, plz Help Me Sir
java yesterday - Date Calendar
java yesterday  Afternoon,Yesterday i have a question that how... a calender from another windwo use java script package rajanikant.code.sep_2008... = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime
calendar
calendar  I got one requirement like this i have to display a calendar with only current month and whenever user selects the date, that date needs to be display in a text field
calendar
calendar  sir, how to fix calendar in javascript..pls provide the complete source code..   Please visit the following link: http://www.roseindia.net/javascript/javascript-calendar.shtml
java again - Date Calendar
java again  I can't combine your source code yesterday, can you help... dt1, String dt2) throws ParseException{ Date date1;// = new Date(); Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat
Java Program - Date Calendar
Java Program  A java program that display calendar.  Hi Friend, Try the following code: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.
continue java - Date Calendar
date1;// = new Date(); Calendar calendar = Calendar.getInstance
Java programme - Date Calendar
Java programme  Can you provide me the coding for Date class provided by java util package
java - Date Calendar
java   Using java how can i change the date format of the system from mm/dd/yy to dd/mm/yy. The code which i wrote is changing the format only on the console and not on the system's control panel.Can you suggest me anything
java - Date Calendar
Java convert string to date  I need an example in Java to convert the string to date.  Convert string to date formatimport java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.*;import java.text.
java yesterday - Date Calendar
java yesterday  And how to make date format DD/MM/YYYY,thank's  Hi friend, Code to help solve the Problem : import java.util.Date; import java.text.SimpleDateFormat; public class SimpleFormatDate { public
ask java - Date Calendar
but in java i found too but i can't to join in another frame. And i want to ask how... information on Java visit to : http://www.roseindia.net Thanks  
calendar - Date Calendar
Creating a Date calendar in Java  I am looking for a code for Creating a Date calendar in Java
calendar - Struts
Java calendar example  Please explain the Java calendar example
ModuleNotFoundError: No module named 'attach'
ModuleNotFoundError: No module named 'attach'  Hi, My Python... 'attach' How to remove the ModuleNotFoundError: No module named 'attach'... to install padas library. You can install attach python with following command
ModuleNotFoundError: No module named 'attach'
ModuleNotFoundError: No module named 'attach'  Hi, My Python... 'attach' How to remove the ModuleNotFoundError: No module named 'attach'... to install padas library. You can install attach python with following command
ModuleNotFoundError: No module named 'attach'
ModuleNotFoundError: No module named 'attach'  Hi, My Python... 'attach' How to remove the ModuleNotFoundError: No module named 'attach'... to install padas library. You can install attach python with following command
Java Calendar Example
The following Java Calendar Example will discuss java.util.Calender class... to display various values. Code of Java Calendar program: import java.util.Date... of the calendar class returns a Date object. It is then passed to the println
this is my code java - Date Calendar
) throws ParseException{ Date date1;// = new Date(); Calendar calendar
A java application to import outlook calendar data
A java application to import outlook calendar data   Hi, I am Thamarai, i was allocated a task to create a java program to import outllok calander, As I am new to java can any one help me to complete my task
Java Date conversion - Date Calendar
Java Date conversion  How do I convert 'Mon Mar 28 00:00:00 IST 1983' which is a java.util.Date or String to yyyy/MM/dd date format in sql
Java Script Code of Calendar and Date Picker or Popup Calendar
Java Script Code of Calendar and Date Picker or Popup Calendar...; This is detailed java script code that can use for Calendar or date picker or popup... that contains all the java script code that has used in calendar. 3: calendra.css
Add year to Date without using Calendar in java
Add year to Date without using Calendar in java  how could i add a year to a date function without using calendar functions
i want to create dynamic calendar in java
i want to create dynamic calendar in java  i want code and explanation
i want to create dynamic calendar in java
i want to create dynamic calendar in java  i want code and explanation
ModuleNotFoundError: No module named 'django-attach'
ModuleNotFoundError: No module named 'django-attach'  Hi, My... 'django-attach' How to remove the ModuleNotFoundError: No module named 'django-attach' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-attach'
ModuleNotFoundError: No module named 'django-attach'  Hi, My... 'django-attach' How to remove the ModuleNotFoundError: No module named 'django-attach' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-attach'
ModuleNotFoundError: No module named 'django-attach'  Hi, My... 'django-attach' How to remove the ModuleNotFoundError: No module named 'django-attach' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-attach'
ModuleNotFoundError: No module named 'django-attach'  Hi, My... 'django-attach' How to remove the ModuleNotFoundError: No module named 'django-attach' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-attach'
ModuleNotFoundError: No module named 'django-attach'  Hi, My... 'django-attach' How to remove the ModuleNotFoundError: No module named 'django-attach' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-attach'
ModuleNotFoundError: No module named 'django-attach'  Hi, My... 'django-attach' How to remove the ModuleNotFoundError: No module named 'django-attach' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-attach'
ModuleNotFoundError: No module named 'django-attach'  Hi, My... 'django-attach' How to remove the ModuleNotFoundError: No module named 'django-attach' error? Thanks   Hi, In your python
attach file in my web page
attach file in my web page  hide button in input typ=file button in jsp
How to get day from date in Java using Calendar?
How to get day from date in Java using Calendar?  Hi, I have a calendar object in my Java program. How I can get the day of date from this? How to get day from date in Java using Calendar? Thanks   Hi, If you don't
Attach a Image fiel to Webservice - WebSevices
Attach a Image fiel to Webservice  My requirement is like this. I... to this String I need to attach an Image file to this. (That is when client...   We can attach a file or an object or a collection to the webservice. Use
Java with Ajax calendar - Java Interview Questions
Java with Ajax calendar  Make a Java web application that will give the month and year for all days that fall in a specific day of the month. For example a user will enter day of the week ( e.g. Monday )and day of the month
calendar js
calendar js  there are two calendar on form and i want that once date chosen from first calendar , the second calendar should be disabled till that date. for eg once i have chosen date 21-03-2012 from first calendar
Gregorian calendar class
Gregorian calendar class  Hai... What is the purpose of gregorian calendar class?   Java provides the standard calendar by providing the class GregorianCalendar in java.util package. The Gregorian calendar is used
java - Date Calendar
Java - Date Calendar
continue java - Date Calendar

Ads