7 Replies - 708 Views - Last Post: 12 October 2011 - 06:35 AM Rate Topic: -----

#1 noobkillerKJ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 09-August 11

How to force a user to input a certain character?

Posted 11 October 2011 - 02:05 PM

I'm suppose to ask the user to enter a time. The user must enter minutes and seconds. For my assignment, in order to get extra marks I must force the user to input numbers only. If the user entered the character "a" the character must delete automatically. Is there a way to do this in java?
Is This A Good Question/Topic? 0
  • +

Replies To: How to force a user to input a certain character?

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: How to force a user to input a certain character?

Posted 11 October 2011 - 02:10 PM

Perfectly possible in a GUI, not quite as easy on the console. (unless you mean removing it after the user has pressed enter)
Was This Post Helpful? 0
  • +
  • -

#3 noobkillerKJ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 09-August 11

Re: How to force a user to input a certain character?

Posted 11 October 2011 - 02:12 PM

Could you guide me with the steps please? I'm doing it in a GUI.
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: How to force a user to input a certain character?

Posted 11 October 2011 - 02:39 PM

Probably the easiest and best way is to use a JSpinner. With a number model, it only accepts numbers, with a date moel, it only accepts dates.

From Oracle's tutorial:
http://download.orac...ts/spinner.html

Quote

SpinnerNumberModel
The SpinnerNumberModel supports sequences of numbers which can be expressed as double objects, int objects, or Number objects. You can specify the minimum and maximum allowable values, as well as the step size the amount of each increment or decrement. The Year spinner uses this model, created with the following code:
SpinnerModel model =
        new SpinnerNumberModel(currentYear, //initial value
                               currentYear - 100, //min
                               currentYear + 100, //max
                               1);                //step

SpinnerDateModel
The SpinnerDateModel supports sequences of Date objects. You can specify the minimum and maximum dates, as well as the field (such as Calendar.YEAR) to increment or decrement. Note, however, that some types of look and feel ignore the specified field, and instead change the field that appears selected. The Another Date spinner uses this model, created with the following code:
Date initDate = calendar.getTime();
calendar.add(Calendar.YEAR, -100);
Date earliestDate = calendar.getTime();
calendar.add(Calendar.YEAR, 200);
Date latestDate = calendar.getTime();
model = new SpinnerDateModel(initDate,
                             earliestDate,
                             latestDate,
                             Calendar.YEAR);

Was This Post Helpful? 2
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,026
  • Joined: 05-April 11

Re: How to force a user to input a certain character?

Posted 11 October 2011 - 02:59 PM

If you are using a textfield, then you can set a document that the textfield should use. In this document you can tell it to only allow numbers. Search for JTextField numbers only document or something like that :)
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to force a user to input a certain character?

Posted 11 October 2011 - 05:35 PM

View PostnoobkillerKJ, on 11 October 2011 - 05:05 PM, said:

Is there a way to do this in java?

Not much you can't do in Java, and actually in any computer language, even in COBOL. Just post your exact requirements
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,438
  • Joined: 27-December 08

Re: How to force a user to input a certain character?

Posted 11 October 2011 - 05:41 PM

View PostCasiOo, on 11 October 2011 - 05:59 PM, said:

If you are using a textfield, then you can set a document that the textfield should use. In this document you can tell it to only allow numbers. Search for JTextField numbers only document or something like that :)

In fact, there is a snippet on DIC demonstrating how to use a Document to restrict input in a JTextField. :)
Was This Post Helpful? 1
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: How to force a user to input a certain character?

Posted 12 October 2011 - 06:35 AM

Last time I suggested using a JSpinner for something like this, the op was also really keen on using a text field too. I can't understand it. Why does the world hate spinners?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1