Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,288 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,074 people online right now. Registration is fast and FREE... Join Now!




Date Picker

 

Date Picker

lover2000

24 Apr, 2009 - 03:56 AM
Post #1

New D.I.C Head
*

Joined: 6 Apr, 2009
Posts: 10

Hi,

is there any easy way to create a java bean which will act like a combo box to get the date in dd-mm-yyyy format. I tried to create a bean but I am struggling with it.
CODE

package cboDate;

/**
*
* @author Kishor
*/
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Date;
import java.util.Calendar;
import javax.swing.JOptionPane;
public class cboDate extends javax.swing.JPanel {

    /** Creates new form cboDate */
    public cboDate() {
        initComponents();
    }
    protected String dd;

    public String getDD() {
        return dd;
    }

    public void setDD(String dd) {
        this.dd = dd;
    }
    protected String mm;

    public String getMM() {
        return mm;
    }

    public void setMM(String mm) {
        this.mm = mm;
    }
    protected String yyyy;

    public String getYYYY() {
        return yyyy;
    }

    public void setYYYY(String yyyy) {
        this.yyyy = yyyy;
    }


    /** 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() {

        cboMM = new javax.swing.JComboBox();
        cboDD = new javax.swing.JComboBox();
        txtYYYY = new javax.swing.JTextField();

        cboMM.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" }));

        cboDD.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        cboDD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboDDActionPerformed(evt);
            }
        });

        txtYYYY.setAlignmentX(0.0F);
        txtYYYY.setAlignmentY(0.0F);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(cboDD, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cboMM, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtYYYY, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboDD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboMM, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                    .addComponent(txtYYYY, 0, 0, Short.MAX_VALUE)))
        );
    }// </editor-fold>

    private void cboDDActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:

    }                                    

  
protected void paintComponent( Graphics g )
    {
        g.setColor( getForeground() );

       int h=cboMM.getHeight()+0;
       int w=cboMM.getWidth();// txtYYYY.getWidth()+10;
       txtYYYY.setPreferredSize(new Dimension(w, h));
       Calendar cal=Calendar.getInstance();

       //int dd=cal.DAY_OF_MONTH;// cal.get(Calendar.DATE);
       cboDD.setSelectedItem(dd);
       cboDD.setSelectedItem(this.dd);
       cboMM.setSelectedItem(this.mm);
       txtYYYY.setText(this.yyyy);
       }

    // Variables declaration - do not modify
    private javax.swing.JComboBox cboDD;
    private javax.swing.JComboBox cboMM;
    private javax.swing.JTextField txtYYYY;
    // End of variables declaration

}




User is offlineProfile CardPM
+Quote Post


g00se

RE: Date Picker

25 Apr, 2009 - 05:20 AM
Post #2

D.I.C Lover
Group Icon

Joined: 19 Sep, 2008
Posts: 1,141



Thanked: 132 times
My Contributions
Use JCalendar

http://www.toedter.com/en/jcalendar/index.html
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 12:15PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month