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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:42:44+00:00 2026-06-14T15:42:44+00:00

I have a swt text box with which I have a key listener assigned.

  • 0

I have a swt text box with which I have a key listener assigned.
I want that the first character in the text box should not be the space
for that I am using the following code –

  textCSVFileName.addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent e){
            if(textCSVFileName.getCharCount() == 0){

                if(e.keyCode == 32){

                }
            }
        }

now I know when the first character is entered by user is space.
but if the above condition is true then how can I restrict it from entering in the textbox?
Any help?

  • 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-14T15:42:45+00:00Added an answer on June 14, 2026 at 3:42 pm

    Although the question is answered with a dirty workaround*, I would suggest having a look at the SWT.Verify event, listening to that and preventing spaces at the first position:

    final Text textField = new Text(shell, SWT.BORDER);
    
    textField.addListener(SWT.Verify, new Listener() {
    
        @Override
        public void handleEvent(Event e) {
            // get old text and create new text by using the Event.text
            final String oldS = textField.getText();
            String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
    
            if(newS.length() > 0 && newS.charAt(0) == ' ')
                e.doit = false;
        }
    });
    

    This will prevent a space at the beginning in any case.


    *The “workaround” does not even work. Just think about what happens when you enter a space at the first position, when the text already contains some text. That will not be prevented by that approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java SWT GUI with a multiline Text control. I want to
I have written an SWT UI which has a primary function of displaying text
I have an SWT/JFace application that uses the Realm (not sure of the concept)
I have a SWT app that opens a OpenGL window (using the LWJGL library)
I currently have a Table [org.eclipse.swt.widgets.Table] with several TableColumns; however, due to UI space
I am building an application in swt, I also have a thread running which
I have been told and have read that SWT Objects must be explicitly disposed
I have a class of this type org.eclipse.swt.widgets.Text . It lives inside of an
I have an SWT application which is quite complex at the moment. And I
My question is that I have a SWT Table in Eclipse RCP project. I

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.