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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:50:29+00:00 2026-06-14T11:50:29+00:00

When I press a key for some time, the background color of the key

  • 0

When I press a key for some time, the background color of the key doesn’t restore to original color in keyReleased function.What am I doing wrong. If you could redirect me to some site contain tutorial on how to make programs like this with examples, it will be very helpful. Here is the code:

public void keyPressed(KeyEvent event)
    {
        int code= event.getKeyCode();
        String x,h;

        for(int i=0;i<names.length;i++)
        {
            s=names[i];
            x=s.trim();

            if(x.contentEquals(event.getKeyText(code)) )
            {
                backColor=but[i].getBackground();
                but[i].setBackground(Color.RED);
                break;
            }
        }
        switch(event.getKeyCode())
        {
        case KeyEvent.VK_BACK_SLASH:
            h=" \\ ";
            for(int i=0;i<but.length;i++)
            {
                if(h.contentEquals(but[i].getText()))
                {
                    backColor=but[i].getBackground();
                    but[i].setBackground(Color.RED);
                    buttonIndex=i;
                    break;
                }
            }
            break;
    }
public void keyReleased(KeyEvent event)
    {
        String x;
        int code= event.getKeyCode();

        for(int i=0;i<names.length;i++)
        {
            s=names[i];
            x=s.trim();

            if(x.contentEquals(event.getKeyText(code)) && !s.contentEquals("                             "))
            {System.out.println("outside");
                but[i].setBackground(backColor);
                break;
            }
            else
                if(s.contentEquals("                             "))
                {
                    but[buttonIndex].setBackground(backColor);
                }
        }
    }
  • 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-14T11:50:30+00:00Added an answer on June 14, 2026 at 11:50 am
    • don’t use Keylistener for Swing JComponents, use KeyBindings instead

    • your JFrame, JPanel, JComponent must be focusable (setFocusable)

    • in one moment only one of JComponent can be focusable, then your program will be focus_hunter this not caused by using KeyBindings

    • for better help sooner post an SSCCE, short, runnable, compilable

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

Sidebar

Related Questions

What I'm trying to achieve is to handle some complex key press and release
when we press a key and keep pressing it the keypress and keydown event
I want to simulate many key press events. I found a solution by using
I want to code a key press event to a text box to accept
I want to disable a particular key press in .NET(both VB.NET & C#.NET).``
The Problem I want to press a key when I have a line highlighted
When I click menu strip and then press shortcut key it works but it
Go Here Use UP and DOWN keys When I press a key, the red
I'm writing an app where I need to simulate key press events on a
I can't figure out how to capture the Delete key press. I found out

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.