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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:47:32+00:00 2026-06-10T05:47:32+00:00

Someone from stackoverflow helped me with my code for buttons by giving me the

  • 0

Someone from stackoverflow helped me with my code for buttons by giving me the right code that’s also more appropriate… But the problem is I want to use this code in a hangman program, when someone clicks on the button it must scan a word and print either “Yes, the letter is in the word” or “NO, the letter is not in the word”

I came across some code in stackoverflow that does that, but I have no idea how to adapt that persons code to my button code

The code for creating buttons(as I got it):

public CharSearch(){

super(BoxLayout.Y_AXIS);
    for(char i = 'A'; i <= 'Z'; i++){
        String buttonText = new Character(i).toString();
        JButton button = getButton(buttonText);
        add(button);
    }
}

public JButton getButton(final String text){
    final JButton button = new JButton(text);
    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "You have clicked: "+text);
            //If you want to do something with the button:
            button.setText("Clicked"); // (can access button because it's marked as final)
        }
    });
    return button

and then the code I found (I’ll also post the link):

if(original.indexOf(button)!=-1){
    JOptionPane.showConfirmDialog(panel, "Your word does contain" + button );
}
else{
    JOptionPane.showConfirmDialog(panel, "There is no" + button );
}

How can I check if a single character appears in a string?

Question is how do I make the two codes work together, I Have tried fusing them but then i get errors with the indexOf at

if(original.indexOf(button)!=-1){

I’ll also post my full code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public final class CharSearch extends Box{
int i =0;
int error = 0;
static JPanel panel;
String original = "Dinosaur";
JLabel label = new JLabel();
String secret = new String(new char[original.length()]).replace('\0', '-');

public CharSearch(){

super(BoxLayout.Y_AXIS);
    for(char i = 'A'; i <= 'Z'; i++){
        String buttonText = new Character(i).toString();
        JButton button = getButton(buttonText);
        add(button);
    }
}

public JButton getButton(final String text){
    final JButton button = new JButton(text);
    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "You have clicked: "+text);
            //If you want to do something with the button:
            button.setText("Clicked"); // (can access button because it's marked as final)
        }
    });
    return button;

    if(original.indexOf(button)!=-1){
    JOptionPane.showConfirmDialog(panel, "Your word does contain" + button );
    }
 else{
    JOptionPane.showConfirmDialog(panel, "There is no" + button );
 }

}

public static void main(String[] args){
    EventQueue.invokeLater(new Runnable(){
        public void run(){
           JFrame frame=new JFrame();
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.setContentPane(new CharSearch());
            frame.pack();
            frame.setVisible(true);
            new CharSearch();
        }
    });
}
}
  • 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-10T05:47:34+00:00Added an answer on June 10, 2026 at 5:47 am

    Change button to button.getText() (assuming the text of the button is just like b or c), or possibly getName if you have them all named by their character (that may be better as it allows you to use different stuff for the text the button shows, like the button could be ‘Choose c’, but the name ‘c’, and it would still work)

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

Sidebar

Related Questions

On some StackOverflow question about GWT cons and pros from 2009 someone answered that:
I got this code from someone and it works very well, I just want
I have the following code, (taken from here), but it causes a stackoverflow exception
Someone from stackoverflow recently helped me form this sql query to return the data
I heard from someone that the maximum array size in .NET is 4 GB?
I got this code from someone, it's almost perfect to create a dynamic breadcrumb,
Thanks to stackoverflow i finally understood that i need to compile from the source
From the sounds of this StackOverflow answer it seems that when I publish my
I have started recently at the recomandation of someone from stackoverflow to use nrComm
I have got lots of ideas from google and stackoverflow- but none of those

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.