Article View: borland.public.jbuilder.java.api
Article #3241Re: A Swing-Based DateField and TimeField
From: Bill Bercik
Date: Wed, 25 Mar 1998 00:00
Date: Wed, 25 Mar 1998 00:00
232 lines
7870 bytes
7870 bytes
Hi Folks,
Never mind my previous post. I have found an undocumented Date and Time
Field that is better than mine that comes bundled with Swing 1.01.
There is a class called DateChooser that is found in the src.zip archive
in the com/sun/swing/pending directory. With a little modification of the
source code it is possible to get this Date and Time Field component to
work with Swing.
Here are some hints:
1)Change the inheritance of DateChooser from Container to JComponent
2)Change the methods used to reference the combo box to current Swing 1.0
methods.
combo.addPossibleValue() becomes combo.addItem().
combo.setCurrentValueIndex() becomes combo.getSelectedIndex()
3) Add the missing methods to the SyncTypeComboBoxEditor class of which
your compiler will complain about. These can be do-nothing methods.
4) Only uses the TINY size for this component. The other sizes have
problems.
That's it.
Bill Bercik wrote:
> I have a feeling that I am not the only one that needs a Time and Date
> field that is Swing based.
>
> I've included some code that works below. Let me know if you can make
> any improvements to it.
>
> -Bill Bercik
>
> import java.awt.*;
> import com.sun.java.swing.*;
> import com.sun.java.swing.border.*;
> import com.sun.java.swing.plaf.basic.*;
> import com.sun.java.swing.event.*;
> import java.util.*;
> class TimeField extends JPanel{
> Spinner hourSpinner;
> Spinner minuteSpinner;
> JComboBox amPmCombo = new JComboBox();
> JLabel colon = new JLabel(":");
> public TimeField(){
> init();
> setCurrentTime();
> }
> public TimeField(GregorianCalendar calendar){
> init();
> setTime(calendar);
> }
> public TimeField(Date date){
> init();
> setTime(date);
> }
>
> public void init(){
> amPmCombo.addItem("AM");
> amPmCombo.addItem("PM");
> this.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
> hourSpinner = new Spinner(1);
> hourSpinner.setDigits(2);
> hourSpinner.setMinimum(1);
> hourSpinner.setMaximum(12);
>
> minuteSpinner = new Spinner(1);
> minuteSpinner.setDigits(2);
> minuteSpinner.setMinimum(0);
> minuteSpinner.setMaximum(59);
>
> this.add(hourSpinner);
> this.add(colon);
> this.add(minuteSpinner);
> amPmCombo.setPreferredSize(new Dimension(48,20));
> this.add(amPmCombo);
> hourSpinner.setToolTipText("Use arrow keys to change values.");
> minuteSpinner.setToolTipText("Use arrow keys to change values.");
> setBorder(BorderFactory.createLineBorder(Color.black,1));
> }
>
> public void setEnabled(boolean b){
> hourSpinner.setEnabled(b);
> minuteSpinner.setEnabled(b);
> colon.setEnabled(b);
> amPmCombo.setEnabled(b);
> }
>
> public void setCurrentTime(){
> GregorianCalendar calendar = new GregorianCalendar();
> hourSpinner.setValue(calendar.get(Calendar.HOUR));
> minuteSpinner.setValue(calendar.get(Calendar.MINUTE));
> if (calendar.get(Calendar.AM_PM) == Calendar.AM){
> amPmCombo.setSelectedIndex(0);
> }else{
> amPmCombo.setSelectedIndex(1);
> }
> }
>
> public void setTime(Date date){
> GregorianCalendar calendar = new GregorianCalendar();
> calendar.setTime(date);
> hourSpinner.setValue(calendar.get(Calendar.HOUR));
> minuteSpinner.setValue(calendar.get(Calendar.MINUTE));
> if (calendar.get(Calendar.AM_PM) == Calendar.AM){
> amPmCombo.setSelectedIndex(0);
> }else{
> amPmCombo.setSelectedIndex(1);
> }
> }
>
> public void setTime(GregorianCalendar calendar){
> hourSpinner.setValue(calendar.get(Calendar.HOUR));
> minuteSpinner.setValue(calendar.get(Calendar.MINUTE));
> if (calendar.get(Calendar.AM_PM) == Calendar.AM){
> amPmCombo.setSelectedIndex(0);
> }else{
> amPmCombo.setSelectedIndex(1);
> }
> }
>
> public GregorianCalendar getTime(){
> int hour = hourSpinner.getValue();
> int minute = minuteSpinner.getValue();
> GregorianCalendar calendar = new GregorianCalendar();
> calendar.set(1,1,1,hour,minute);
> if (amPmCombo.getSelectedIndex() == 0){
> calendar.set(Calendar.AM_PM,Calendar.AM);
> }else{
> calendar.set(Calendar.AM_PM,Calendar.AM);
> }
> return calendar;
> }
>
> }
>
> class DateField extends JPanel{
> Spinner monthSpinner;
> Spinner daySpinner;
> JLabel slash1 = new JLabel("/");
> JLabel slash2 = new JLabel("/");
> Spinner yearSpinner;
>
> public DateField(){
> init();
> setCurrentDate();
> }
>
> public DateField(GregorianCalendar calendar){
> init();
> setDate(calendar);
> }
>
> public DateField(Date date){
> init();
> setDate(date);
> }
>
> public void init(){
> this.setLayout(new FlowLayout(FlowLayout.CENTER,1,1));
> monthSpinner = new Spinner(1);
> monthSpinner.setDigits(2);
> //monthSpinner.setFont(new Font("Dialog", 1, 10));
> monthSpinner.setMinimum(1);
> monthSpinner.setMaximum(12);
>
> daySpinner = new Spinner(1);
> daySpinner.setDigits(2);
> daySpinner.setMinimum(1);
> daySpinner.setMaximum(32);
> //JLFSpinnerUI spinnerUI2 = JLFSpinnerUI.createUI(daySpinner);
> //spinnerUI2.installUI(daySpinner);
> //daySpinner.setUI(spinnerUI2);
>
> yearSpinner = new Spinner(1);
> yearSpinner.setMinimum(1998);
> yearSpinner.setMaximum(2025);
> yearSpinner.setDigits(4);
> this.add(monthSpinner);
> this.add(slash1);
> this.add(daySpinner);
> this.add(slash2);
> this.add(yearSpinner);
> yearSpinner.setToolTipText("Use arrow keys to change values.");
> monthSpinner.setToolTipText("Use arrow keys to change values.");
> daySpinner.setToolTipText("Use arrow keys to change values.");
> setBorder(BorderFactory.createLineBorder(Color.black,1));
> }
> public void setEnabled(boolean b){
> daySpinner.setEnabled(b);
> monthSpinner.setEnabled(b);
> slash1.setEnabled(b);
> slash2.setEnabled(b);
> yearSpinner.setEnabled(b);
> }
>
> public void setCurrentDate(){
> GregorianCalendar calendar = new GregorianCalendar();
> daySpinner.setValue(calendar.get(Calendar.DATE));
> monthSpinner.setValue(calendar.get(Calendar.MONTH)+1);
> yearSpinner.setValue(calendar.get(Calendar.YEAR));
> }
>
> public void setDate(Date date){
> GregorianCalendar calendar = new GregorianCalendar();
> calendar.setTime(date);
> daySpinner.setValue(calendar.get(Calendar.DATE));
> monthSpinner.setValue(calendar.get(Calendar.MONTH)+1);
> yearSpinner.setValue(calendar.get(Calendar.YEAR));
> }
>
> public void setDate(GregorianCalendar calendar){
> daySpinner.setValue(calendar.get(Calendar.DATE));
> monthSpinner.setValue(calendar.get(Calendar.MONTH)+1);
> yearSpinner.setValue(calendar.get(Calendar.YEAR));
> }
>
> public GregorianCalendar getDate(){
> int day = daySpinner.getValue();
> int month = monthSpinner.getValue()-1;
> int year = yearSpinner.getValue();
> GregorianCalendar calendar = new GregorianCalendar();
> calendar.set(year,month,day);
> return calendar;
> }
>
> }
Technical Details
Message-ID:
<351940F4.A8D9C485@remoove_this.dippybird.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<35174C1A.5D2EFA4D@remoove_this.dippybird.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,408770dea5ded4f X-Google-Attributes: gid1089d0,public From: Bill Bercik <bill@remoove_this.dippybird.com> Subject: Re: A Swing-Based DateField and TimeField Date: 1998/03/25 Message-ID: <351940F4.A8D9C485@remoove_this.dippybird.com> X-Deja-AN: 337593056 Content-Transfer-Encoding: 7bit References: <35174C1A.5D2EFA4D@remoove_this.dippybird.com> Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.api