the project does not need to look exactly like professor garrett's. intead, you can have the input at the top (north), the 12 buttons in the center, and the 2 buttons at the bottom (south) for clear and send. the 12 buttons in the center can be arranged as border layout also. examples are in the book or on the cd. good luck, will be starting on this asap
I got the form all made with all the buttons, but has anyone figured out yet how to call to the listener from a different class?? I just don't know the syntax.
I started off with one big class that contained all of my variables and objects, then put smaller classes inside the big class. That way all of my listeners and methods could access all of the variables and objects. My program works but I am going to try to separate the classes instead of having just one big one. I think we might have to pass (by reference) some objects such as the text of the display text box between the different classes??
Remember that you can reference objects in other classes within the same project by referencing the classname followed by the object. So this can be done using separate classes. When I created it, I used 3 different classes: The class that had the frame, the class that had the borders, and the class that had the buttons. Listener classes were attached to the border class and the class with the buttons, but sometimes you have to reference objects that are not defined in the class where the listener is, i.e. in the class with the buttons I had a listener for the buttons but it had to append the appropriate digit to the text in a textbox that was an object instatiated in the border class.
I didn't put any restrictions on how this project could be accomplished, so do whatever works for you, as nddissi mentions above.
i am having trouble. when i click a button, my label where the numbers should show up disappears. when i clear it comes back. no numbers show up tho. any reason for this? also, after clearing, it no longer does this when clicking buttons, but still no numbers
I have my program setup in 3 classes (the driver or main program, a border class, and a grid class). I have two listeners, one in each layout class, but for some reason I am having trouble passing the input results from the grid layout back to the border layout class. Just curious if anyone could throw me a bone, cause other than that, my program is looking splendid.
I've got my classes created, but I am having trouble with a couple things. First, I don't know how to add the keypad (which is a grid layout for me) to the main panel (border layout). Second, I could not find pop-ups in the book and cannot figure out how to create the pop-up or a listener for the SEND button to initiate a pop-up. Any help on either of these problems would be great. Thanks
as for the rest of the code I just used the book examples, made a basic JFrame setup and had the call in my first class file which called the second.. and everything else is in my second.. The layout setup (I used grid), my action listener, and the popups. (layout managers starts on page 341)
I have a seperate actionlistener for each button and code to add the numbers to the text box and run the pop up is within the listeners.
Don't know if there's a better way, but this is working fine for me, and I'm happy with that since I missed the last 8 weeks of class..
I just have 2 action listeners. I have one listener for all the keypad buttons. Inside the listener it determines which button was clicked and then adds that character to the text field. I have one listener for the send and clear buttons, it also determines which one was clicked and either displays the dialog box or clears the text field.
Is there a way to access the textfield in another class without making the textfield static?
Inside of my listener I tried: PhonePanel.display.setText() ... but it said a non-static variable could not be accessed from a static context. Is there a different syntax that we are supposed to use?
I've got my classes separated into three as I think most of you do. My number buttons are arranged into a grid layout and added to my border layout class. However, I am still having problems with getting my text field to update when I use the number pad from a separate class. Any specific help anyone is willing to give would be awesome.
Your textbox needs to be declared as static, declared at the very beginning of the class (before the constructor). That way you can reference it by classname.objectname. Here is an excerpt from a web page that describes static variables (aka "class variables"):
Class Variables When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables. In the case of the Bicycle class, the instance variables are cadence, gear, and speed. Each Bicycle object has its own values for these variables, stored in different memory locations. Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
For example, suppose you want to create a number of Bicycle objects and assign each a serial number, beginning with 1 for the first object. This ID number is unique to each object and is therefore an instance variable. At the same time, you need a field to keep track of how many Bicycle objects have been created so that you know what ID to assign to the next one. Such a field is not related to any individual object, but to the class as a whole. For this you need a class variable, numberOfBicycles, as follows:
public class Bicycle{
private int cadence; private int gear; private int speed;
// add an instance variable for the object ID private int id;
// add a class variable for the number of Bicycle objects instantiated private static int numberOfBicycles = 0; ...... }
Class variables are referenced by the class name itself, as in Bicycle.numberOfBicycles
This makes it clear that they are class variables.
Instead of using a static text field, I took the advice from chris and went ahead and passed the text field through the constructor of my grid layout. This ended up working quite well and eliminates some problems with the static text field.
I still can not get the text field to display. I also have three classes with two button listeners. I have my Frame, My Grid class (which has my digits and button listener for digits), and my panel class that contains my border layout. How do you pass results from the button listener in the grid class to the text field in the panel class.
The concept of the passed parameter is a great alternative to the static (class) variable, i.e. passing an argument by reference to a constructor gives the constructor the address of the object (the text box), which it, in turn, assigns an instance variable to. Since the instance variable now points to the address of the textbox that was passed in, the constructed object can freely access that memory location and can also write to it. The difference is that the original textbox doesn't need to be declared as a static. Using either approach will work: Use whichever one is easier for you.
Maybe Mike can elaborate on how he did it with the reference variable passed to the constructor for those that might want to try that method (it might require some redesign, though).
Is there a way to retrieve the name of the button as a string (as in btnOne = new JButton ("1") where "1" is the name) and have one line of code within the listener instead of 12 lines?
I was thinking:
event.getSource().getName (returns a compile error)
or
JButton source = event.getSource() source.getName (cant convert object to JButton)
or something along those lines. It would be along cleaner than how I did it.
Something I found that I do not recall seeing in the book is how to turn a text field into a output only area. I found this usefull because I did not want the user to be able to type into the text box, and instead only click on the buttons to set the phone number. Since I use textfield.getText in my program to get the number, allowing the user to type in the textfield allows them to add non-numbers to the phone number when the send button was pressed. If this project was really dialing a phone number then allowing a user to put say an "A" into the number could cause an error.
So to turn the textfield into an output only area of the gui all you need to do is (with a JTextField named textfield) textfield.setEditable(false);
one downside to this is it changes the background color of the textfield to grey, but you can change it back to white with the setBackground method.
This is what i did: made the textfield static created a static method in the border class that accepts a string variable this method then uses the string and adds it to the previous entry in the textbox and displays it called the static method from the if statements in the listenere of the grid class i found this method to work well
As long as the program does what it is suppose to do that is the bare minimum which is on the project 3 worksheet. Everything else is for your own benefit, I believe.
kdtaylor, no i don't think you have to limit the program like I did.
I just wanted to share that you could make the textbox output only. The real reason i even tried to do it was because in VB i never could get a text box to only be output without the program graying out the textbox, which didn't look right. So I'm sorry if I confused you, but i just wanted to share that it could be done.
Depending on how you design your program it could be useful to leave the Text Field editable. I implemented my listeners one at a time starting with the Send/Clear buttons, so when I was testing them the only way to see if they worked was to type in the Text Field.
what advantages would there be to create a textbox and set editable to false? wouldnt it have been the same as creating a label? is there any situation where a textbox.setEditable(false) would be better to use than a label?
I have a question, in my layout, i have a small bar underneath my gridbox and above my clear button that is the color of the background, and i tried editing the dimensions of the gridbox and it isn't being removed. Just wanted to see if anyone had a problem with this and a solution for it.
I haven't had that layout problem, and this is how i set up my panels: For my grid panel I set up a 3 by 4 grid layout and then I added the buttons to the panel. For the border layout sections i put the text box in the North, the grid panel in the Center, send button in the East, and clear button in the South. When my program runs there is no background showing, only the textbox and all the buttons.
When I place restrictions on how something needs to be done, it is stated in the assignment. If I don't say something needs to be done a specific way, you have complete flexibility to do it however you would like. The bottom line is that the program has to meet the written specifications (including the General Specifications). Beyond that, it's totally up to you.
I never specified in this assignment how it needed to be done, so use whatever approach makes sense to you. As is obvious from these postings, you have chosen a variety of approaches, and that's fine. Keep in mind, though, that design should come before coding, so whatever approach you decided to use needs to be justified and documented in your documentation bundle.
Just to make sure I am doing this correctly. Rather than submitting the source code to the website, we are attaching the code in an email to Professor Garrett?
i know this is late but, i wanted to let everyone know how i did it. i just declared the buttons in my numpad as static and my if statement called for each button seperately, like "numpad.num1" without the prefix, it didnt know what button i was talking about.