Clear contents of a textarea. Please help! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Clear contents of a textarea. Please help!

SeamoSeamo Posts: 42Member
Hi,
I'm trying to clear the contents of a textarea and it won't happen for me. Can anyone help. I am just creating a simple calculator to the specifications of semantic algebra and I can't clear the textarea. If any one can help I would be very gratefull. I will put the code I have written and it won't compile because of the command dispaly.clear()

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;


public class Calculator1 extends JFrame implements ActionListener{

// ----------------------- FIELDS --------------------------

private TextArea display = new TextArea();
private JButton[] button = new JButton[17];

static String[] keys = {" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 ",
" 7 "," 8 "," 9 "," + "," * "," , ","If","LastAnswer"," Total ", "ON"};

private boolean truth = false;

private StringBuffer memory = new StringBuffer();
private StringBuffer reg1 = new StringBuffer();
private StringBuffer reg2 = new StringBuffer();

private char operator;
private String operator2;
private boolean firstNumberAlreadyInput = false;

public Calculator1() {

Container c = getContentPane();

c.setLayout(new BorderLayout());
c.add(display,BorderLayout.NORTH);
JPanel keypad = new JPanel();
keypad.setLayout(new GridLayout(4,4));

// Set up Keypad

for (int index=0;index!=17;index++) {
button[index] = new JButton(keys[index]);
button[index].addActionListener(this);
keypad.add(button[index]);
}
c.add(keypad,BorderLayout.SOUTH);
setSize(600,600);
pack();
show();

}



//implement actionlisteners for buttons
public void actionPerformed(ActionEvent event) {
final int positionOfTotal = 15;
Object source = event.getActionCommand();

if (testForDigit(source)) return;
else {
if (testForOperator1(source)) return;
else{
if (testForOperator2(source)) return;
else{
if(testForOperator3(source))return;
else {
//display.setText(doCalculation());
//register.setLength(0);
firstNumberAlreadyInput = false;
}}
}
}
}

//methods

/* Test for digit: test for digit in the range 0-9 */

private boolean testForDigit(Object source) {

for (int digit=0;digit<10;digit++) {
if (source.equals(keys[digit])) { // Found digit
if (firstNumberAlreadyInput) {
//register.append(String.valueOf(digit));
display.append(String.valueOf(digit));
//display.setText(register.toString());
}
else {
//register.append(String.valueOf(digit));
//display.setText(register.toString());
display.append(String.valueOf(digit));
}
return(true);
}
}

return(false);
}

/* Test for operator: */

private boolean testForOperator1(Object source) {
for (int digit=10;digit<13;digit++) {
if (source.equals(keys[digit])) {
operator = keys[digit].charAt(1);
display.append(String.valueOf(operator));
firstNumberAlreadyInput = true;
return(true);
}
}

return(false);
}



private boolean testForOperator2(Object source) {
for (int digit=13;digit<14;digit++) {
if (source.equals(keys[digit])) {
//operator = String.valueOf(digit);
String one = "If";
display.append(one);

firstNumberAlreadyInput = true;
return(true);
}
}

return(false);
}



private boolean testForOperator3(Object source) {

if (source.equals(keys[17])) {
display.clear();
repaint();
firstNumberAlreadyInput = false;
return(true);
}
return(false);
}




/* Do calculation: Includes divide by zero error test. */

/* private String doCalculation() {

try {
int numA = new Integer(register.toString()).intValue();

switch(operator) {
case '+':
return String.valueOf(numA+numB);
case '*':
return String.valueOf(numA*numB);
default:
return String.valueOf(numA/numB);
}
}
catch (ArithmeticException error) {
System.out.print(beep);
return " E R R O R";
}
}
}*/





public static void main(String[] args)
{
Calculator1 test = new Calculator1();

test.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}





Comments

  • AshlungsAshlungs Posts: 9Member
    : Hi,
    : I'm trying to clear the contents of a textarea and it won't happen for me. Can anyone help. I am just creating a simple calculator to the specifications of semantic algebra and I can't clear the textarea. If any one can help I would be very gratefull. I will put the code I have written and it won't compile because of the command dispaly.clear()


    : display.clear();

    Take a look at the api for TextArea.
    http://java.sun.com/j2se/1.5.0/docs/api/index.html
    The method clear() is not in the java api for TextArea nor is it inherited from TextComponent, Component, or Object so you need to use:

    display.setText("");//clears textArea by setting to null String

    or extend TextArea and add your own clear() method, which does this if you are going to use it alot, as it makes the programing more intuitive to a future reader. But for one use....

    Hope this helps.
    Ashlungs
  • SeamoSeamo Posts: 42Member
    Thanks that sorted that problem out.

    : : Hi,
    : : I'm trying to clear the contents of a textarea and it won't happen for me. Can anyone help. I am just creating a simple calculator to the specifications of semantic algebra and I can't clear the textarea. If any one can help I would be very gratefull. I will put the code I have written and it won't compile because of the command dispaly.clear()
    :
    :
    : : display.clear();
    :
    : Take a look at the api for TextArea.
    : http://java.sun.com/j2se/1.5.0/docs/api/index.html
    : The method clear() is not in the java api for TextArea nor is it inherited from TextComponent, Component, or Object so you need to use:
    :
    : display.setText("");//clears textArea by setting to null String
    :
    : or extend TextArea and add your own clear() method, which does this if you are going to use it alot, as it makes the programing more intuitive to a future reader. But for one use....
    :
    : Hope this helps.
    : Ashlungs
    :

Sign In or Register to comment.