Results 1 to 7 of 7
  1. #1
    semoche is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default need help with my calculator

    i'm designing a simple calculator and i need to add a backspace button(using jbuttons). have written methods to take care of the other buttons but i dont have a backspace button method. need some ideas. thanks

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Some ideas on what?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    semoche is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    i need some ideas on how to write a method that can allow me erase an unwanted number on the textfield

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    I think we need a little more information. Is this a function calculator? If it is, you could pop the number/operation off the stack and set the textfield as stack.peek(). If it is not, you could setText("");

  5. #5
    semoche is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    the other calculator buttons work. i wrote the method below to handle numbers 1,2,3...
    but it doesn't work for the backspace button.i need it to work the same way a keyboard backspace works.

    public void assign(String n)
    {
    if((textField.getText()).equals("0"))
    textField.setText(n);
    else{
    n = textField.getText()+""+n;
    textField.setText(n);
    }
    }
    so for number 1, i just call assign("1"); an so on.

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    I don't see what the problem is. It sounds like you just need to take the text in the Text Field and remove the last character.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Well although your code for other character works that is NOT the best way to do it. The proper way would be to update the Document. That is what normally happens every time you type a new character into a text field. So what you should be using is:

    Java Code:
    textField.getDocument().insertString(....);
    Similiarly to remove a character from the text field you should update the Document:

    Java Code:
    textField.getDocument().remove(...);

Similar Threads

  1. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 07:00 AM
  2. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 01:57 PM
  3. Calculator program
    By kevzspeare in forum New To Java
    Replies: 6
    Last Post: 03-18-2009, 02:43 PM
  4. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 08:42 AM
  5. Swing Calculator
    By nemo in forum AWT / Swing
    Replies: 1
    Last Post: 05-28-2007, 12:07 PM

Posting Permissions

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