Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2005

Tip: Looking for answers? Try searching our database.

JSpinner Query

Thread view: 
Abhijeet Iraj - 07 Mar 2005 13:29 GMT
Hi,

 I am trying to create timefield using JSpinner similar to TimeField in
Date/Time Properties dialog in Microsoft Windows. Here is the code i have
written

/*****************************************/

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

public class MySpinner {
  JFormattedTextField tf;

  public MySpinner(){
   JFrame frame = new JFrame("Spinner");
   frame.setDefaultCloseOperation(3);

   final SpinnerDateModel model = new SpinnerDateModel();
   JSpinner spinner = new JSpinner(model);
   JSpinner.DateEditor editor = new
   JSpinner.DateEditor(spinner,"HH:ss:mm");
   spinner.setEditor(editor);

   tf =((JSpinner.DateEditor)spinner.getEditor()).getTextField();
   tf.setEditable(true);
   tf.setBackground(Color.white);
   tf.setSelectionColor(Color.blue);
   tf.setSelectedTextColor(Color.white);
   DefaultFormatterFactory factory =
      (DefaultFormatterFactory)tf.getFormatterFactory();
   DateFormatter formatter =
      (DateFormatter)factory.getDefaultFormatter();
   formatter.setAllowsInvalid(false);

   frame.getContentPane().add(spinner, BorderLayout.SOUTH);
   frame.pack();
   frame.show();
 }

 public static void main (String args[]) throws Exception  {
   new MySpinner();
 }

}

/*******************************************/

It works just fine when i use up/down keys or spinner at the right to change
the time. But when i try to
modify the time by editing the TimeField it is not behaving as expected.
I want it such that Timefield should allow user to change the time using
Spinner as well as by editing the
timefield. But it should not allow the user to enter invalid time.

Please give me your suggestions. Am I missing something here?

Thanks in advance.

Abhijeet
Dee Dee - 21 Mar 2005 02:01 GMT
Try changing the line:
formatter.setAllowsInvalid(false);

to

formatter.setCommitsOnValidEdit(false);

The second line doesn't check the values that are entered until the
focus leaves the spinner or until the user hits enter.

> Hi,
>
[quoted text clipped - 61 lines]
>
> Abhijeet


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.