Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JSpinner with time bounds

JörgOct 5 2008 — edited Oct 6 2008
Hello,

in the following code the second spinner cannot be spun.
But removing the editor makes it spinnable again. (Click on the hours first.)
Any idea why - respectively how the format can be maintained?
import java.awt.*;
import java.util.*;
import javax.swing.*;
 
public class TimeSpinner extends JFrame {
  public TimeSpinner() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLayout(new GridLayout(2,2, 0,5));
    JSpinner spin24 = new JSpinner(new SpinnerDateModel());
    spin24.setEditor(new JSpinner.DateEditor(spin24, "HH:mm:ss"));

    Calendar start= Calendar.getInstance();
    start.set(Calendar.HOUR_OF_DAY, 8);
    start.set(Calendar.MINUTE, 0);
    Calendar middle= Calendar.getInstance();
    middle.set(Calendar.HOUR_OF_DAY, 12);
    middle.set(Calendar.MINUTE, 30);
    Calendar end= Calendar.getInstance();
    end.set(Calendar.HOUR_OF_DAY, 17);
    end.set(Calendar.MINUTE, 0);
System.out.println(start.getTime());
System.out.println(middle.getTime());
System.out.println(end.getTime());
    SpinnerDateModel spinModel= new SpinnerDateModel(middle.getTime(),
		start.getTime(), end.getTime(), Calendar.MINUTE);
    JSpinner boundedSpinner = new JSpinner(spinModel);
    boundedSpinner.setEditor(new JSpinner.DateEditor(boundedSpinner,"HH:mm"));

    add(new JLabel("24 hours spinner"));
    add(spin24);
    add(new JLabel("8 to 17 hours spinner"));
    add(boundedSpinner);
    pack();
    setVisible(true);
  }


  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
	new TimeSpinner();
      }
    });
  }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 3 2008
Added on Oct 5 2008
8 comments
533 views