wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes basic telephone keypad Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basic telephone keypad" Watch "basic telephone keypad" New topic
Author

basic telephone keypad

joey whitewolfe
Greenhorn

Joined: Sep 07, 2002
Posts: 11
how do I build a basic java application of a telephone keypad and add a label at the bottom saying what I want it to say? I've tried and this is what I've come up with so far but it doesn't work
my code
[ edited to preserve formatting using the [code] and [/code] UBB Tags -ds ]
[ September 07, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
how do I build a basic java application of a telephone keypad and add a label at the bottom saying what I want it to say?
I haven't looked at your code yet. Are you basically curious about how to change the displayed text of a label?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
joey whitewolfe
Greenhorn

Joined: Sep 07, 2002
Posts: 11
i want to know how do I make this work,a functional telephone keypad with the label at the bottom of the pad could you please look over my code and help me? I am using 1.3.1platform2 I just want to do it right and make it work it doesn't have to actually dial a real number, just be based on one. It is a class project and I need help it will compile but won't run. joey
joey whitewolfe
Greenhorn

Joined: Sep 07, 2002
Posts: 11
when i try and run it i get these errors in dos
java.lang.nullpointer
java.awt.container.addimpl(container.java;340
223
45
339
479
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Let's start by considering the error message:So, it's telling us that we've a problem that probably originates at line 36 or 93.
Line 36 is
keyPad.add(keysArray[i]);
and line 93 is
Telephone teleFrame = new Telephone();
Lines 36 through 48 ( keyPad.add(keysArray[12]); ) would appear to be trying to use components of an array that haven't been properly initialized. Take a good look at the code that creates and uses the array of buttons. Do you ever actually create and add new Button objects to the array? The answer is no. Do you have any ideas on how to correct this problem?
[ September 08, 2002: Message edited by: Dirk Schreckmann ]
joey whitewolfe
Greenhorn

Joined: Sep 07, 2002
Posts: 11
totally lost
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Another interesting thing is to look at how many Buttons you can put in a keyArray, and check how many you are putting into it, or taking out of it.
Also how do you know what Buttons do what? Like mine at home got digits on them. In fact I don't see any Buttons, just places to put buttons!
I know we are pi**g you around but when you see it you will be a better programmer for it
-Barry
[ September 08, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
joey,
What part is causing you confusion? Is working with arrays new for you? Are you comfortable making programs with Buttons when the Buttons aren't part of an array?
joey whitewolfe
Greenhorn

Joined: Sep 07, 2002
Posts: 11
yes I'm new to arrays just confussing me on the concepts
joey whitewolfe
Greenhorn

Joined: Sep 07, 2002
Posts: 11
when i try and run in dos i get a main thread error- is there any reason it won't run in dos?i can compile it but when i type java Telephone i get the thread error
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
A few posts up, I displayed the error message that this program creates and I began to explain why this error message is created.
You may want to take some time to read and learn about using arrays in Java. The following list of sites are all decent places to have a look for such information:
  • Sun's Java Tutorial
  • Introduction to Computer Science using Java by Bradley Kjell
  • Introduction to Programming Using Java by David J. Eck
  • Dick Baldwin's Java Programming Tutorials

  • In a nutshell, the aforementioned error message is displayed because you have an array of Button references that don't refer to actual Button objects. You've never created any Buttons - you've only created an array of Button references (that all refer to null). Perhaps you want to use a loop structure to create new Button objects for the array of Button references to refer to.
    joey whitewolfe
    Greenhorn

    Joined: Sep 07, 2002
    Posts: 11
    thanks I got it now!
    joey whitewolfe
    Greenhorn

    Joined: Sep 07, 2002
    Posts: 11
    thanks guys! I was able to get it running and just wanted you to see the finished code
    /*
    Name:
    Date:September 4, 2002
    Program Name:Telephone
    Purpose:Web Phone
    */
    import java.awt.*;
    import java.awt.event.*;
    public class Telephone extends Frame implements ActionListener
    {
    public Button keysArray[];
    public Panel keyPad;
    public TextField displayField;
    public boolean foundKey;
    public Label bottomLabel;
    public Telephone()
    {
    displayField = new TextField(20);
    keyPad = new Panel();
    keysArray = new Button[12];
    Label bottomLabel = new Label("Click each button above to dial your number");
    displayField.setEditable(false);
    setBackground(Color.magenta);

    //Set Frame Layout
    setLayout(new BorderLayout());
    keyPad.setLayout(new GridLayout(4,3,10,10));
    //Creating Buttons
    for (int i = 0; i <= 9; i++)
    keysArray[i] = new Button(String.valueOf(i));
    keysArray[10] = new Button("*");
    keysArray[11] = new Button("#");

    //adding buttons
    for (int i = 1; i <= 9; i++) //adds buttons 1-9 to Panel
    keyPad.add(keysArray[i]);
    keyPad.add(keysArray[10]);//adds * button to Panel
    keyPad.add(keysArray[0]);//adds 0 button to Panel
    keyPad.add(keysArray[11]);//adds # button to Panel

    add(displayField, BorderLayout.NORTH); //adds text field to top of Frame
    add(keyPad, BorderLayout.CENTER);//adds the keypad
    add(bottomLabel, BorderLayout.SOUTH);//adds the display at the bottom
    for(int i = 0; i < keysArray.length; i++)
    keysArray[i].addActionListener(this);
    addWindowListener(
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );
    }//end of Telephone method
    public void actionPerformed(ActionEvent e)
    {
    foundKey = false;
    //Search fo the key pressed
    for (int i = 0; i < keysArray.length && !foundKey; i++)
    if(e.getSource() == keysArray[i])//key match found
    {
    displayField.setText(displayField.getText() + keysArray[i].getLabel());
    }
    }
    public static void main(String args[])
    {
    //Create a new instance of the Telephone object
    Telephone teleFrame = new Telephone();
    //Set frame attributes
    teleFrame.setBounds(50,100,260,300);
    teleFrame.setTitle("Telephone");
    teleFrame.setVisible(true);
    }
    }
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    Thanks for sharing what you learned. That is the key element that makes JavaRanch so great and helps others to learn.
    Note that when posting code, much of the formatting is preserved by surrounding the code with the [code] and [/code] UBB Tags.
    Hope to see you 'round the Ranch!
    Barry Gaunt
    Ranch Hand

    Joined: Aug 03, 2002
    Posts: 7729
    Great, Joey! There's nothing better than getting
    the "AhHa" after a few pushes and shoves.
    -Barry
    [ September 15, 2002: Message edited by: Barry Gaunt ]
    Ron Newman
    Ranch Hand

    Joined: Jun 06, 2002
    Posts: 1056
    Dirk, how did you manage to post the actual UBB tags in your message without them being interpreted?


    Ron Newman - SCJP 1.2 (100%, 7 August 2002)
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    What, that wasn't on the SCJP exam you took?
    Try using the appropriate html tags. The left bracket is &#91; and the right bracket is &#93; . Take a look at http://www.asciitable.com to find out what different ones are.
    Enjoy and use this power wisely...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: basic telephone keypad