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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:58:59+00:00 2026-06-09T21:58:59+00:00

So I have a basic swing application, it contains a JTextfield , I add

  • 0

So I have a basic swing application, it contains a JTextfield, I add the following KeyListener to it:

public class PromptKeyListener implements KeyListener {

    private boolean SHIFT_PRESSED;

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            //Do Thing A
        } else if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
            this.SHIFT_PRESSED = true;
        }

                     if (SHIFT_PRESSED
                && e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
            //Do Thing B
        }

       }

    @Override
    public void keyReleased(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
            this.SHIFT_PRESSED = false;
        }
        System.out.println(SHIFT_PRESSED);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

}

The problem is when I press and hold SHIFT as well as Backspace Thing B does happen but the Backspace key does also remove a single character from the textbox as it normally does.

Is there a way to prevent Backspace from removing characters from the textbox when SHIFT is held down?

  • 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-09T21:59:01+00:00Added an answer on June 9, 2026 at 9:59 pm

    See Stopping default behavior of events in Swing

    That answer says you should be able to call

    e.consume()
    

    to prevent the default behavior of text going into the field.

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

Sidebar

Related Questions

I have to setup a basic JAVA application including a GUI based on swing.
I have a basic web service that returns the following object as JSON: public
I have a basic nServicebus 3.2.0 setup with a web application endpoint and a
I have a basic web application written in Java, running on a tomcat on
I have developed a basic Chat application in Java. It consists of a server
I have this basic Java application in witch dim_x and dim_y represent the dimensions
I'm creating a basic Swing applcation. I have jTextPane and a button. If the
As the title suggests, we have a Java (Swing) desktop application, and we'd like
I have a Swing application running on Java 1.6.0_20 on Windows XP (32-bit). I
I have one JFrame that's not actively rendered, a la standard basic Swing applications,

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.