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

  • Home
  • SEARCH
  • 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 8482007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:42:49+00:00 2026-06-10T19:42:49+00:00

Get letter pressed from JButton when pressed public class ButtonDisabler implements ActionListener { @Override

  • 0

Get letter pressed from JButton when pressed

  public class ButtonDisabler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btnGetText = (JButton) e.getSource();
        char charLetterPressed;            
        charLetterPressed=(btnGetText.getText().charAt(1));

        btnGetText.setEnabled(false);
    }
}

Then use that letter and compare it to a string, then display the letter only if found into a JLabel

 char charChkWord;
     StringBuffer word = new StringBuffer();
    for (int i = 0; i < strRandomWord.length(); i++) {
        charChkWord = strRandomWord.charAt(i);
        if (charLetterPressed == String.valueOf(charChkWord)) {
            lblWord.setText(word.append(charChkWord).toString());
        }
    }

I’m not sure how to get that letter and compare it to the string.

  • 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-10T19:42:51+00:00Added an answer on June 10, 2026 at 7:42 pm

    I’m with trashgod, I’d avoid KeyListeners if you can.

    I’d also set the text of the button to the character you want to use and/or set the name of the button as well

    JButton btnA = new JButton("A");
    btnA.setName("A");
    

    This would allow you to make a choice over how you want to display the text on the button while providing you a means for providing additional information that might be more useful to you…

    JButton button = (JButton) evt.getSource();
    String text = button.getText();
    // If you wanted to use the name of the button instead...
    String name = button.getName();
    
    // You would use this if you need part of the text...
    char charPressed = Character.toLowerCase(text.charAt(0)); 
    // You could to this to convert the character to a String for easier
    // comparison...
    String strCharPressed = Character.toString(text.charAt(0)).toLowerCase(); 
    
    // A sample
    String word = "This is a test";
    
    // Finds the first occurrence of the character in the String...
    // Comparison is case sensitive...
    // If indexOf > -1 then the word contains the character
    int indexOf = word.toLowerCase().indexOf(charPressed);
    // Or, you just check to see if it is contained in the word...
    boolean contains = word.toLowerCase().contains(Character.toString(charPressed));
    
    System.out.println("indexOf = " + indexOf);
    System.out.println("contains = " + contains);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Get class from div inside an li and add to the same li. The
I want to get the drive letter of pendrive or external harddisk connected through
I am trying to get the current index of the letter that typed in
I want to get the index of a particular letter, e.g. > match(LETTERS,G) [1]
When I enter a letter for one of the numbers I get an error:
I'd like to get this user's OS language as a ISO-639-2 (3 letter language
Is there a short formula to get the n-th letter of the alphabet? For
Hi I need a Regex that can actually get any letter that is inserted
I am trying to use a formula to get a letter of the alphabet.
When I use this function in c#, it is able to get drive letter

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.