<< Return to Labs Page

Eclipse - How to add a Button

These directions are written specifically to help you add a button to the ArrayStats.java program for CS 1 lab 7. This does make for a nice description for how to add graphical Buttons in Java in general, though. Also note that there are descriptions of how to work with buttons and with other graphical user interface (GUI) objects in the course textbooks.

    What Are Buttons?

  1. To start, just realize that a Button in Java is just another object. It's just like the very common String objects that you've seen before. Just because a Button object happens to have a graphical representation doesn't mean it doesn't behave like any other object. Each one is represented by a variable, and we have to declare it (say what type our variable is), initialize it (give it a value), and the object then stores some data and has some methods available for us to use.
  2. Here's a look at where the other buttons are defined in the ArrayStats.java file:
  3. To create your own button variable, just a new variable name onto the end of the line, after bClear. I'm going to name my Button variable bStdDev just so I'll have something to refer to. So, my line would then look like:
  4. The next thing we have to do is initialize our new variable. Since we're working within an Applet this will happen inside the init() method. This is a method that gets called once when the Applet is first created and started. That's exactly what we want for our own initialization.
  5. In the lines of code above we see several Button variables being initialized. Just like with any other object, we have to call the new keyword variable, and then we call the constructor method for this type of object.
  6. In case you need a refresher, a constructor method is a special method that each type of object has that's used to create that object. The constructor method just always has the same name as the object it's meant to "construct". So, this does things like find some computer memory where your object can be stored and take in parameters (i.e. data values) that this object type requires. For example, a String object needs to take in the line of text characters, a string literal, which it will represent. Similarly, we see that each Button constructor method also takes in a string literal. This is the text that will be displayed on the surface of the graphical button. Remember that the button we're creating requires the text: "Standard Deviation".
  7. If you haven't already, add a line of code to initialize your new Button object. It would probably make the most sense to add this right under the last button initialization line (for the variable bClear).
  8. Seeing and Hearing Buttons

  9. With our Button declared and initialized, we now have 2 things left to do that are specific to these GUI objects: (1) set up a listener and (2) add the GUI object to something that can display it.
  10. First, let's set up the listener and figure out what that even means. Since we're creating a button, we want people using our program to be able to click on that button... and then have our code respond appropriately. Well, if our code is going to respond as it should, we have to somehow link our button to the code that should be called when the button is clicked. This is what we're doing by adding a listener. We're declaring what piece of code, really what method, will be called when the button is clicked. Here's the code that was doing that for the other buttons:
  11. The addActionListener() method works like any other method you may call: it takes in parameters and it then performs some action. Here this method takes in a single parameter, which is the object where we have code that we want to respond to clicks on this button. We're using the this key word to say that we want this particular object to repond to button clicks. (Yes, ArrayStats.java will be forming an object, just like you have String objects and Button objects. Don't worry about this. For now just think of it as referencing "this file of code.")
  12. Be sure to add your own line of code adding an action listener for the new button that you're creating.
  13. Alright, one more thing: making our button visible. We do this just by calling the add() method and giving it the variable representing our button. There are other, more complicated ways of doing this. This simpler method works fine for us.
  14. In my example, I'm adding the button next-to-last so that the answer text field will still come up last.
  15. Responding To Our Button

  16. Ok, fine, we've made a button. Now exactly what code will be run when the button is clicked? Answer: the actionPeformed() method.
  17. There's more going on here than we need to worry about. Just notice that there's an if statement corresponding to each of the different buttons. Now we'll just add another if statement for our button and within the code body for that if statement we'll call our stdDev() method. Here's a code example for how to do that:
  18.    if (cause == bStdDev)
          int[] temp = new int[nextFree];
          for(int i=0; i<temp.length; i++){
             temp[i] = (int) list[i];
          mAnswer.setText("The standard deviation is " + stdDev(temp));
  19. Finally, here's what the entire method looks like with that new code added in (with the top portion missing because the picture ran out of space):
  20. Now, after all that work, we should have a functioning ArrayStats.java Applet. Email your TA or any of the CS 1 course staff if you have further questions.