Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how to check if a button has been pressed?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to check if a button has been pressed?

    so i have a simple typing game where the user clicks the start button and 10 random words appear at the top of the screen. The user then has to enter these words into the texbox and press the space bar to move onto the next word. When the user is finished typing the words a message box appears saying how many errors were made.

    my problem is if the user doesn't press the start button and just starts typing in the textbox and presses the space bar the words start to change at the top of the screen. i only want the words to change if the start button has been pressed.

    heres the code to change the words:
    int code = evt.getKeyCode();
     
            String text=wordfield.getText();
     
            if(code==evt.VK_SPACE) //moves to next word if space is pressed
             {
                this.jTextArea1.setText("");
                if(this.WordCounter<10)
                {
                  generateNextWord();
                 this.WordCounter++;
                }
            else
               {
                this.jTextArea1.setEnabled(false);
                JOptionPane.showMessageDialog(this, this.errorCount, "Game over! Errors made: ", JOptionPane.INFORMATION_MESSAGE);
     
              }
                return;
            }
            String entered=this.jTextArea1.getText();
           //check for errors
            if(entered.length()<text.length())
            {
                int positionToCheck=entered.length()-1;
                if(text.charAt(positionToCheck)!=entered.charAt(positionToCheck))
                {
                    errorCount++;
                    Toolkit.getDefaultToolkit().beep();
                }
            }

    so basically i just need code that checks if the start button has been pressed then do the code above else don't do that code


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: how to check if a button has been pressed?

    Add an ActionListener to the button that sets a boolean, and then just check against that boolean.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to check if a button has been pressed?

    i already have some code in an ActionPerformed event for the button. how do i add an ActionListener?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how to check if a button has been pressed?

    First hit from a web search for "java ActionListener": How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    --- Update ---

    Did you read the links about cross-posting the last time you did it? I recommend doing so

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/65344-problem-typing-game.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. Pausing a loop until a button is pressed
    By amcqueen in forum AWT / Java Swing
    Replies: 1
    Last Post: October 5th, 2011, 01:57 AM
  2. Trying to start a game when 'Enter' is pressed.
    By Shaybay92 in forum AWT / Java Swing
    Replies: 6
    Last Post: September 27th, 2011, 07:48 AM
  3. Is Key Pressed statements
    By Yo Cas Cas in forum AWT / Java Swing
    Replies: 6
    Last Post: August 27th, 2011, 12:48 AM
  4. How to code "check button"
    By Suni in forum AWT / Java Swing
    Replies: 1
    Last Post: December 28th, 2010, 09:21 AM
  5. Move button with mouse pressed
    By Stavros in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 10th, 2010, 08:45 AM