2
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;

public class nava extends JFrame implements ActionListener
{
    Button b1=new Button("clear");
    TextField t1=new TextField();

    public nava()
   {
        this.setLayout(new GridLayout(2,2));
        b1.addActionListener(this);
        this.setSize(200,200);

        add(t1);
        add(b1);

    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
        {
            t1.setText("");   //without space
        }
    }   

    public static void main(String r[])
   {
        new nava().show();
    }   
}

On clicking the button b1 the TextField should get empty but it is not getting empty.The textfield just remain the same .But when I put space in the actionPerformed function it add a space in textfield. Please tell me what is the problem with the code.

public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
        {
            t1.setText(" "); //with space
        }
    }      
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Use Swing components, not AWT components. For example use JButton and JTextField not Button and TextField. Also don't call deprecated methods such as `show()`. Please consider checking the Swing tutorials as they'll teach you much you'll find useful. – Hovercraft Full Of Eels Oct 24 '15 at 14:48
  • 1
    As an aside, you will want to learn and use [Java naming conventions](http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java). Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others. – Hovercraft Full Of Eels Oct 24 '15 at 14:50

1 Answers1

1

Use Swing components, not AWT components and the code should work. For example use JButton and JTextField not Button and TextField. Also don't call deprecated methods such as show(). Please consider checking the Swing tutorials as they'll teach you much you'll find useful. For example:

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

@SuppressWarnings("serial")
public class Nava extends JFrame implements ActionListener {
    JButton b1 = new JButton("Clear");
    JTextField t1 = new JTextField("Fubar", 10);

    public Nava() {
        this.setLayout(new GridLayout(2, 2));
        b1.addActionListener(this);
        // this.setSize(200, 200);

        add(t1);
        add(b1);

        b1.setMnemonic(KeyEvent.VK_C); // alt-c to activate button
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {
            t1.setText(""); 
        }
    }

    public static void main(String r[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Nava nava = new Nava();
                nava.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                nava.pack(); // let components and layout managers size themselves
                nava.setLocationByPlatform(true);
                nava.setVisible(true);
            }
        });
    }
}

As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.

As for why your code doesn't work -- I honestly don't know as I don't work directly with AWT library components; very few people do. Edit: note this similar question. The authors of that question don't know why setText("") but offer a work-around:

// first this:
tf.setText(" "); 

// followed by this:
tf.setText("");
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373