Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8377391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:39:38+00:00 2026-06-09T15:39:38+00:00

I have a JTextField . And when the user enters a or j ,

  • 0

I have a JTextField. And when the user enters a or j, I want the text within the text field to be upper-case (e.g. enter “ab”, output “AB”). And if the first letter is not one of the following,

  • a, t,j, q, k, 2, 3, …,9

I don’t want the text field to display anything.

And here’s what I have,

public class Gui {
    JTextField tf;
    public Gui(){
        tf = new JTextField();
        tf.addKeyListener(new KeyListener(){
           public void keyTyped(KeyEvent e) {
           }
           /** Handle the key-pressed event from the text field. */
           public void keyPressed(KeyEvent e) {
           }
           /** Handle the key-released event from the text field. */
           public void keyReleased(KeyEvent e) {
           }
        });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:39:40+00:00Added an answer on June 9, 2026 at 3:39 pm

    You can override the method insertString of the Document class. Look at an example:

    JTextField tf;
    
    public T() {
        tf = new JTextField();
        JFrame f = new JFrame();
        f.add(tf);
        f.pack();
        f.setVisible(true);
    
        PlainDocument d = new PlainDocument() {
            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                String upStr = str.toUpperCase();
                if (getLength() == 0) {
                    char c = upStr.charAt(0);
                    if (c == 'A' || c == 'T' || c == 'J' || c == 'Q' || c == 'K' || (c >= '2' && c <= '9')) {
                        super.insertString(offs, upStr, a);
                    }
                }
    
            }
        };
        tf.setDocument(d);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a text field to enter an amount of money. I want
I have a JTextField with some text. When I click text field the cursor
I have a question about text field. I am writing to let the user
I have a textfield where the user can type what ever he want into
I have a JTextField that I'm not always passing a value to, but it
I have a JFrame with some a JTextField and a JButton. I want it
I have a bunch of UITextFields where a user must enter a number (the
I have a page where some products and textfields where the user enters a
I have created the output for a program that allows a user to input
I have one url which i have to hit every time whenever user enters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.