Pages

Clear JTextArea text

Complete source code below will show you, how to clear text in JTextArea.

************************************************************************
COMPLETE SOURCE CODE FOR : ClearJTextAreaText.java
************************************************************************


import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.BorderLayout;

public class ClearJTextAreaText implements ActionListener
{
//Create text area with initial text ( PUT YOUR TEXT HERE )
JTextArea textArea=new JTextArea("PUT YOUR TEXT HERE");

//Create button with text ( Click here to clear text )
JButton button=new JButton("Click here to clear text");

//Create layout that will be use by JFrame
BorderLayout bl=new BorderLayout();

//Create window using JFrame with title ( Clear JTextArea text )
JFrame frame=new JFrame("Clear JTextArea text");

//Constructor
public ClearJTextAreaText()
{
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

button.addActionListener(this);

frame.setLayout(bl);
frame.add(textArea,BorderLayout.CENTER);
frame.add(button,BorderLayout.SOUTH);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,500);
frame.setVisible(true);
}

//override actionPerformed method
public void actionPerformed(ActionEvent event)
{
//Action that will perform when someone click the button
if(event.getSource()==button)
{
//Clear text in JTextArea
textArea.setText("");
}
}

//Main method
public static void main(String[]args)
{
ClearJTextAreaText cjtat=new ClearJTextAreaText();
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************
Related Posts Plugin for WordPress, Blogger...