• We just launched and are currently in beta. Join us as we build and grow the community.

Disable an Input Character in Java

peppe_ux

Authentication Bypass Expert
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
172
Likes
39
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial will teach you how to create a program that will disable a specific input of a character in java.


So, now let's start this tutorial!


1. Open JCreator or NetBeans and make a java program with a file name of disableInputChar.java.

2. Import the swing package library:
  1. import

    javax.swing.*

    ;

    // used to access the JComponent,JFrame,JTextField, and KeyStroke class

3. We will initialize variables in our Main, variable frame as JFrame and txtField as JTextField.
  1. JFrame

    frame =

    new

    JFrame

    (

    "Disable an Inputted Character"

    )

    ;
  2. JTextField

    txtField =

    new

    JTextField

    (

    15

    )

    ;

4. Now, to disable an input character getInputMap method of the textfield with its keystroke that has the typed character and the "none" function, means not to include in the textfield.
  1. txtField.getInputMap

    (

    JComponent

    .WHEN_FOCUSED

    )

    .put

    (
  2. KeyStroke

    .getKeyStroke

    (

    "typed a"

    )

    , "none"

    )

    ;

  3. txtField.getInputMap

    (

    JComponent

    .WHEN_FOCUSED

    )

    .put

    (
  4. KeyStroke

    .getKeyStroke

    (

    "typed 3"

    )

    , "none"

    )

    ;

As what you have seen the code above, the keyword "typed a" here indicates that when we type letter 'a' to the keyboard in the textfield there is a certain action that is invoked. But we have put its partner to "none", so meaning there is no certain action to be invoked and you cannot type this letter in the textfield. The same goes to the number 3.

5. Lastly, add the textfield, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.getContentPane

    (

    )

    .add

    (

    txtField)

    ;
  2. frame.setSize

    (

    300

    , 300

    )

    ;
  3. frame.setVisible

    (

    true

    )

    ;

Output:
disable.png


Here's the full code of this tutorial:

  1. import

    javax.swing.*

    ;

    // used to access the JComponent,JFrame,JTextField, and KeyStroke class

  2. public

    class

    disableInputChar {
  3. public

    static

    void

    main(

    String

    [

    ]

    argv)

    {
  4. JFrame

    frame =

    new

    JFrame

    (

    "Disable an Inputted Character"

    )

    ;
  5. JTextField

    txtField =

    new

    JTextField

    (

    15

    )

    ;


  6. txtField.getInputMap

    (

    JComponent

    .WHEN_FOCUSED

    )

    .put

    (
  7. KeyStroke

    .getKeyStroke

    (

    "typed a"

    )

    , "none"

    )

    ;

  8. txtField.getInputMap

    (

    JComponent

    .WHEN_FOCUSED

    )

    .put

    (
  9. KeyStroke

    .getKeyStroke

    (

    "typed 3"

    )

    , "none"

    )

    ;

  10. frame.getContentPane

    (

    )

    .add

    (

    txtField)

    ;
  11. frame.setSize

    (

    300

    , 300

    )

    ;
  12. frame.setVisible

    (

    true

    )

    ;
  13. }
  14. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top