Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Swing Show Time in JSpinner Component

Related Tutorials


 
 

Share on Google+Share on Google+

Show Time in JSpinner Component

Advertisement
In this section, you will learn about the creation of a JSpinner Component with time.

Show Time in JSpinner Component

     

In this section, you will learn about the creation of a JSpinner Component with time. Here, you will see time is increased or decreased with the help of JSpinner button. This program increases or decreases the time and change hour from 1 to 12. Following image shows you a JSpinner component with time:

Time in a Swing JSpinner Component

If you want to increase or decrease the minute then you have to put the cursor in the minute section of the JSpinner. Following are some methods and APIs:

Date():
This is the constructor of Date class. Date class is used by importing from java.util.*; package of java. This class is used to represent date and time according to Greenwich Mean Time (GMT) by default.

SpinnerDateModel():
It is a constructor of SpinnerDateModel class. It creates a SpinnerDateModel and takes date and Calendar.HOUR_OF_DAY from a calendar. 

Calendar.HOUR_OF_DAY
This is the Calendar class field which returns the hour of the specified time.

JSpinner.DateEditor(spinner, "hh:mm"):
This is the constructor of JSpinner.DateEditor class. It is used to formatting the date given in the JSpinner component. This constructor takes two argument in which, first is the JSpinner specification and second one is the format in "hh:mm" means first come hour and then the minute.

setEditor():
This is the method of the JSpinner class i.e. used to change that component which is helpful to show the current value of the SpinnerModel.

Here is the code of program:

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class ShowTimeJSpinner{
  public static void main(String[] args) {
  ShowTimeJSpinner h = new ShowTimeJSpinner();
  }

  public ShowTimeJSpinner(){
  JFrame frame = new JFrame("Creating JSpinner Component with time");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Date date = new Date();
  SpinnerDateModel sm = 
 
new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY);
  JSpinner spinner = new JSpinner(sm);
  JSpinner.DateEditor de = new JSpinner.DateEditor(spinner, "hh:mm");
  spinner.setEditor(de);
  frame.add(spinner,BorderLayout.NORTH);
  frame.setSize(100,100);
  frame.setVisible(true);
  }
}

Download this program

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Discuss: Show Time in JSpinner Component   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
Sanjaya Liyanage
December 31, 2011
Start time and an end time

I want to define a start and an end time for the spinner.As an example I want to only allow the users to select a time value between 14:00 and 18:00.How can I achieve this?
sunnychadha
November 28, 2012
JSpineer

the above code displays time..but the problem rises when i store the data in ms access using spinners...data is not added..please help me on this
sunnychadha
November 28, 2012
spinner

the above code displays time..but the problem rises when i store the data in ms access using spinners...data is not added..please help me on this
Yuan
May 9, 2013
Thank

Thank you Gracias!!!
DMCA.com