How to use JCalendar date picker in your Java Swing Applications with NetBeans

1:

/*

2:

* To change this template, choose Tools | Templates

3:

* and open the template in the editor.

4:

*/

5:

package

calendar.app;

6:

 

7:

import

java.util.Calendar;

8:

 

9:

/**

10:

*

11:

* @author greenxgene

12:

*/

13:

public

class

DatePickerExample

extends

javax.swing.JFrame {

14:

 

15:

/**

16:

* Creates new form DatePickerExample

17:

*/

18:

public

DatePickerExample() {

19:

initComponents();

20:

}

21:

 

22:

/**

23:

* This method is called from within the constructor to initialize the form.

24:

* WARNING: Do NOT modify this code. The content of this method is always

25:

* regenerated by the Form Editor.

26:

*/

27:

@SuppressWarnings(

"unchecked"

)

28:

// <editor-fold defaultstate="collapsed" desc="Generated Code">

29:

private

void

initComponents() {

30:

 

31:

jPanel1 =

new

javax.swing.JPanel();

32:

outputtextbox =

new

javax.swing.JTextField();

33:

mydatechooser =

new

com.toedter.calendar.JDateChooser();

34:

jLabel1 =

new

javax.swing.JLabel();

35:

jLabel2 =

new

javax.swing.JLabel();

36:

jButton1 =

new

javax.swing.JButton();

37:

 

38:

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

39:

setTitle(

"Date Picker Example"

);

40:

 

41:

jLabel1.setText(

"Choose Date"

);

42:

 

43:

jLabel2.setText(

"Output"

);

44:

 

45:

jButton1.setText(

"Get Date"

);

46:

jButton1.addActionListener(

new

java.awt.event.ActionListener() {

47:

public

void

actionPerformed(java.awt.event.ActionEvent evt) {

48:

jButton1ActionPerformed(evt);

49:

}

50:

});

51:

 

52:

javax.swing.GroupLayout jPanel1Layout =

new

javax.swing.GroupLayout(jPanel1);

53:

jPanel1.setLayout(jPanel1Layout);

54:

jPanel1Layout.setHorizontalGroup(

55:

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

56:

.addGroup(jPanel1Layout.createSequentialGroup()

57:

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

58:

.addGroup(jPanel1Layout.createSequentialGroup()

59:

.addGap(147, 147, 147)

60:

.addComponent(jLabel1))

61:

.addGroup(jPanel1Layout.createSequentialGroup()

62:

.addGap(143, 143, 143)

63:

.addComponent(jButton1))

64:

.addGroup(jPanel1Layout.createSequentialGroup()

65:

.addGap(165, 165, 165)

66:

.addComponent(jLabel2))

67:

.addGroup(jPanel1Layout.createSequentialGroup()

68:

.addGap(123, 123, 123)

69:

.addComponent(mydatechooser, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))

70:

.addGroup(jPanel1Layout.createSequentialGroup()

71:

.addGap(65, 65, 65)

72:

.addComponent(outputtextbox, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)))

73:

.addContainerGap(83, Short.MAX_VALUE))

74:

);

75:

jPanel1Layout.setVerticalGroup(

76:

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

77:

.addGroup(jPanel1Layout.createSequentialGroup()

78:

.addContainerGap()

79:

.addComponent(jLabel1)

80:

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

81:

.addComponent(mydatechooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

82:

.addGap(13, 13, 13)

83:

.addComponent(jButton1)

84:

.addGap(25, 25, 25)

85:

.addComponent(jLabel2)

86:

.addGap(18, 18, 18)

87:

.addComponent(outputtextbox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

88:

.addContainerGap(25, Short.MAX_VALUE))

89:

);

90:

 

91:

javax.swing.GroupLayout layout =

new

javax.swing.GroupLayout(getContentPane());

92:

getContentPane().setLayout(layout);

93:

layout.setHorizontalGroup(

94:

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

95:

.addGroup(layout.createSequentialGroup()

96:

.addContainerGap()

97:

.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

98:

.addContainerGap())

99:

);

100:

layout.setVerticalGroup(

101:

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

102:

.addGroup(layout.createSequentialGroup()

103:

.addContainerGap()

104:

.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

105:

.addContainerGap())

106:

);

107:

 

108:

pack();

109:

}

// </editor-fold>

110:

 

111:

private

void

jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

112:

Calendar cal = mydatechooser.getCalendar();

113:

int

datevar = cal.get(Calendar.DATE);

114:

int

monthvar = cal.get(Calendar.MONTH);

115:

int

yearvar = cal.get(Calendar.YEAR);

116:

outputtextbox.setText(

"Date is "

+ datevar +

", Month is "

+ monthvar +

" and Year is "

+ yearvar +

"."

);

117:

118:

}

119:

 

120:

/**

121:

* @param args the command line arguments

122:

*/

123:

public

static

void

main(String args[]) {

124:

/*

125:

* Set the Nimbus look and feel

126:

*/

127:

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

128:

/*

129:

* If Nimbus (introduced in Java SE 6) is not available, stay with the

130:

* default look and feel. For details see

131:

* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

132:

*/

133:

try

{

134:

for

(javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

135:

if

(

"Nimbus"

.equals(info.getName())) {

136:

javax.swing.UIManager.setLookAndFeel(info.getClassName());

137:

break

;

138:

}

139:

}

140:

}

catch

(ClassNotFoundException ex) {

141:

java.util.logging.Logger.getLogger(DatePickerExample.

class

.getName()).log(java.util.logging.Level.SEVERE, null, ex);

142:

}

catch

(InstantiationException ex) {

143:

java.util.logging.Logger.getLogger(DatePickerExample.

class

.getName()).log(java.util.logging.Level.SEVERE, null, ex);

144:

}

catch

(IllegalAccessException ex) {

145:

java.util.logging.Logger.getLogger(DatePickerExample.

class

.getName()).log(java.util.logging.Level.SEVERE, null, ex);

146:

}

catch

(javax.swing.UnsupportedLookAndFeelException ex) {

147:

java.util.logging.Logger.getLogger(DatePickerExample.

class

.getName()).log(java.util.logging.Level.SEVERE, null, ex);

148:

}

149:

//</editor-fold>

150:

 

151:

/*

152:

* Create and display the form

153:

*/

154:

java.awt.EventQueue.invokeLater(

new

Runnable() {

155:

 

156:

public

void

run() {

157:

new

DatePickerExample().setVisible(true);

158:

}

159:

});

160:

}

161:

// Variables declaration - do not modify

162:

private

javax.swing.JButton jButton1;

163:

private

javax.swing.JLabel jLabel1;

164:

private

javax.swing.JLabel jLabel2;

165:

private

javax.swing.JPanel jPanel1;

166:

private

com.toedter.calendar.JDateChooser mydatechooser;

167:

private

javax.swing.JTextField outputtextbox;

168:

// End of variables declaration

169:

}