Clear JPasswordField text

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

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


import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JFrame;

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

import java.awt.GridLayout;

public class ClearJPasswordFieldText implements ActionListener
{
//Create password field
JPasswordField passwordField=new JPasswordField(12);

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

//Create layout that will be use by JFrame
GridLayout gl=new GridLayout(2,1);

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

//Constructor
public ClearJPasswordFieldText()
{
button.addActionListener(this);

frame.setLayout(gl);
frame.add(passwordField);
frame.add(button);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,130);
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 password in JPasswordField
passwordField.setText("");
}
}

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


*************************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************************

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
************************************************************************

Clear JTextField text

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

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


import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;

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

import java.awt.GridLayout;

public class ClearJTextFieldText implements ActionListener
{
//Create text field with initial text ( PUT YOUR TEXT HERE )
JTextField textField=new JTextField("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
GridLayout gl=new GridLayout(2,1);

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

//Constructor
public ClearJTextFieldText()
{
button.addActionListener(this);

frame.setLayout(gl);
frame.add(textField);
frame.add(button);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,130);
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 JTextField
textField.setText("");
}
}

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


**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************

RELAXING NATURE VIDEO