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 3224146
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:11:20+00:00 2026-05-17T16:11:20+00:00

I want to have a JTextField (or another text-like input which supports focus and

  • 0

I want to have a JTextField (or another text-like input which supports focus and a cursor — or maybe I should code my own?) where I want to have full control over the immediate input. I.e. I want to ignore most inputs and handle the rest specifically. It seems that JFormattedTextField is not what I want because it does the verification not on the immediate input but when the focus is lost.

I tried to add a key listener but it’s a bit non-obvious how I should use it. It seems I can catch up events like cursor changes (via keys) or copy/paste (via key-shortcuts) in keyPressed and I can catch all normal key input in keyTyped. I am not sure how I can catch cursor changes or copy/paste made via mouse (for some reason, right click does not work anyway, so copy/paste via mouse in the context menu seems not possible but I am not sure if that might be a different issue or if it should work and just does not for some reason).

I basically want something like the following handler:

interface InputHandler {
   void addStringAt(int pos, String s);
   void deleteSubString(int pos, int len);
   void setFocusTo(int pos);
}

And it should be absolutely impossible for the user to get any stuff onto the textbox which are not going through this handler.

Right now, it seems like I have to take care about a lot of specific cases (like on some architectures, there may be some context menu where they could copy/paste or input something, handle all mouse events manually, etc).

Or the other way, which also involves a lot of special handlings: To recode such input field myself. This still seems saver because I can know for sure that there are no tricky methods how the user could bypass the handler (and I really must know that for sure). Btw., how would I do this? I thought of extending a JLabel. How can I extend it so that it can get a focus?

I wonder how you would implement something like this.

  • 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-05-17T16:11:21+00:00Added an answer on May 17, 2026 at 4:11 pm

    It sounds like you need a DocumentFilter. A couple of existing questions that will help with this:

    • Cut and Paste in JTextArea
    • Help on writing your own javax.swing.text.Document
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a text field which extends javax.swing.JTextField and also have document listener like this
I have a JDialog which contains JPanel and other elements like JTextField . I
I have written the below code which has a text-field and a button. as
I have a form input text field, and I want to change the text
hi i have to text fields i want to move the focus from first
I have a JFrame with some a JTextField and a JButton. I want it
I have a textfield which displays a result but I don't want it to
I have a textField on my stage named 'adBuy', which when clicked I want
want to have a Hyperlink-Button in a gridView in which I can display a
i have textfield inside html table..i want after i type something like name in

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.