Results 1 to 7 of 7
  1. #1
    MBP9004 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Action Event buttons

    Im trying to make a miniature keypad where the user clicks on a button and it displays the keys they've entered into a JTextField.
    The problems im having is
    1) Getting more than one digit in the JTextField (the answer.setText part)
    2) When i click the buttons it always goes to the last digit in this case the ", "

    Any help would be greatly appreciated :)

    Java Code:
    String[][] keyChars = { { "a", "b", "c" }, { "d", "e", "f" }, { " ", ".", "," } };
    Java Code:
    keypad = new JButton[3][3];
        for (int i = 0; i < 3; i++) {
          for (int j = 0; j < 3; j++)
          {
            keypad[i][j] = new JButton(keyChars[i][j]);
            c.add(keypad[i][j]);
    	     keypad[i][j].addActionListener(
    	  
    	     	new ActionListener()
    	 
    	     {	public void actionPerformed(ActionEvent e)
    	  	
    	     	{	
    	     		for (int l = 0; l < 3; l++) {
          				for (int k = 0; k < 3; k++)
          		{			
    	     		JButton b0 = (JButton) e.getSource();
    				answer.setText(keyChars[l][k]);					
    	     	}
    	     		   }
    	     	}
    	     });
    			
          } 
        }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Action Event buttons

    You're only calling setText(), which replaces the text instead of appending it. You could use the getText() method to keep track of what's already there...

    And your nested for loop simply loops through, setting the text to every keyChar. When you exit the loop, the text will just be the last iteration of the loop.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    MBP9004 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: Action Event buttons

    Yea i see what you mean about the loops but im not sure what the code is for adding to the textfield rather than setText.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,935
    Rep Power
    19

    Default Re: Action Event buttons

    Java Code:
    public void actionPerformed(ActionEvent e) 
    {
       for (int l = 0; l < 3; l++) {
          for (int k = 0; k < 3; k++)  {
             JButton b0 = (JButton) e.getSource();
             answer.setText(keyChars[l][k]);
          }
       }
    }
    You've added this to each button.
    Just follw the logic here.
    It will loop over i and j and set the answer text to that value...each time round the loop...

    The thing is, you already know i and j at this point, so use them.
    Now you can't use them directly (the nature of anonymous inner classes), but you could either add a constructor to your anon class that takes two ints or create temp final ints for i and j (ifin and jfin?).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,935
    Rep Power
    19

    Default Re: Action Event buttons

    Actually, forget that.
    Just get the text from the button!
    Since that's all you seem to be using.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    MBP9004 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: Action Event buttons

    Quote Originally Posted by Tolls View Post
    Actually, forget that.
    Just get the text from the button!
    Since that's all you seem to be using.
    I seem to have that sorted, its the adding them to the JTextField im stuck at, can only seem to do it through the answer.setText(), I'm unaware of the code used to append the JTextfield.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,935
    Rep Power
    19

    Default Re: Action Event buttons

    Kevins already said how to do that, though.
    Use the getText method on the text field.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Help With Java Buttons & Action Listener In A GUI.
    By UnAccomplishedJavaPerson in forum New To Java
    Replies: 9
    Last Post: 11-07-2011, 10:37 AM
  2. refreshing my Buttons after executing an action
    By hariza in forum AWT / Swing
    Replies: 8
    Last Post: 10-18-2010, 03:20 AM
  3. My buttons all perform their action like 100 times
    By 711groove in forum New To Java
    Replies: 0
    Last Post: 12-13-2009, 10:49 AM
  4. how to add event handling to my buttons
    By VinTiger in forum New To Java
    Replies: 2
    Last Post: 05-13-2009, 08:33 AM
  5. Action Event and Listeners
    By lost1 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 04:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •