Saturday, August 24, 2013

JSpinner

JSpinner represents a combination of an input field and a ordered list of values. The user is allowed to type value in the field. He/she also can select a value from the ordered list. The Spinner component can generate change event. So you need to receive the event by implementing the ChangeListner interface.

Constructors of the JSpinner class:
-JSpinner()
creates a spinner with an Integer SpinnerNumberModel.
-JSpinner(SpinnerModel model)
creates a spinner with the specified model. The commonly used sub-classes of the SpinnerModel interface are SpinnerNumberModel and SpinnerDateModel.

Useful methods of the JSpinner class:
-commitEdit():void
commits the curretly edited value to the spinner model.
-getValue():Object
returns the current value of the spinner.
-setEditor(JComponent editor):void
sets the specified editor (supporting input formats) to the spinner.
-setModel(SpinnerMode model):void
sets the specified model to the spinner.


Example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class JSpinnerShow extends JFrame implements ChangeListener{
JLabel lblqty;
JLabel lbldate;
JSpinner numspinner;
JSpinner datespinner;
JSpinnerShow(String title){
setTitle(title);
setSize(500,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new GridLayout(2,1));
JPanel p=new JPanel();
p.setLayout(new GridLayout(2,2));
p.setBorder(BorderFactory.createTitledBorder("Sale Entry"));
SpinnerNumberModel spnummodel=new SpinnerNumberModel(10,1,100,1);
numspinner=new JSpinner(spnummodel);
numspinner.addChangeListener(this);
p.add(new JLabel("Select quantity sold:"));
p.add(numspinner);
Date cdate=new Date();
SpinnerDateModel spdatemodel=new SpinnerDateModel(cdate,null,null,Calendar.DATE);
datespinner=new JSpinner(spdatemodel);
datespinner.setEditor(new JSpinner.DateEditor(datespinner, "dd:MM:yyyy"));
datespinner.addChangeListener(this);
p.add(new JLabel("Select sale date:"));
p.add(datespinner);
contpane.add(p,BorderLayout.CENTER);
JPanel plbl=new JPanel();
plbl.setLayout(new FlowLayout());
lblqty=new JLabel();
lblqty.setForeground(Color.RED);
lbldate=new JLabel();
lbldate.setForeground(Color.RED);
plbl.add(lblqty);
plbl.add(lbldate);
plbl.setBorder(BorderFactory.createTitledBorder("Information"));
contpane.add(plbl);
setVisible(true);

}

public void stateChanged(ChangeEvent e){
JSpinner sp=(JSpinner)e.getSource();
try{
sp.commitEdit();
}catch(ParseException pe){}
if(sp==numspinner)
lblqty.setText("Quantity:"+sp.getValue().toString());
else if(sp==datespinner)
lbldate.setText("Date:"+sp.getValue().toString());
}


}

public class JFrameSwing {
public static void main(String[] args){
new JSpinnerShow("JSpinner");
}
}

JSpinner Swing

In the example program above, the number spinner is created to provide a text field and a selectable list of numbers. The spinner data such as current, min, max, and increment values are defined when the SpinnerNumberModel is created. The second spinner is date spinner to the allow the user to enter a date or select a date from the list.  The SpinnerDateModel object is constructed to provide the current date, lower and upper bound dates, and the size of increase or decrease. By assigning null values to the lower and upper bounds dates, your date spinner will not have lower and upper limit dates. The size of the increase or decrease will be one field of the Calendar class.  It can be one of the followings:

Calendar.ERA
Calendar.YEAR
Calendar.MONTH
Calendar.WEEK_OF_YEAR
Calendar.WEEK_OF_MONTH
Calendar.DAY_OF_MONTH
Calendar.DAY_OF_YEAR
Calendar.DAY_OF_WEEK
Calendar.DAY_OF_WEEK_IN_MONTH
Calendar.AM_PM
Calendar.HOUR
Calendar.HOUR_OF_DAY
Calendar.MINUTE
Calendar.SECOND
Calendar.MILLISECOND

To format the date displays on the text field, you need to set the editor to the date spinner by using the DateEditor sub-class of the JSpinner class. In this example code, the form of the date displays in the text field is dd-MM-yyyy (two-digits day, two-digit month, and four-digit year.

No comments:

Post a Comment