Pages

Get JTextArea text

Source code below will show you how to get text from JTextArea.

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


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

import java.awt.BorderLayout;

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

public class GetJTextAreaText implements ActionListener
{
//Create button with text ( Click me to get JTextArea contents )
JButton button=new JButton("Click me to get JTextArea contents");

//Create JTextArea
JTextArea textArea=new JTextArea();

//Create a window with title ( Get JTextArea text )
JFrame frame=new JFrame("Get JTextArea text");

public GetJTextAreaText()
{
//Set line wrapping for text area to true
textArea.setLineWrap(true);

//Add action listener to button
button.addActionListener(this);

//Set JFrame layout to BorderLayout
frame.setLayout(new BorderLayout());

//Add created text area into JFrame
frame.add(textArea,BorderLayout.CENTER);

//Add created button into JFrame
frame.add(button,BorderLayout.SOUTH);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,500);

//Make JFrame visible
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
//Method getText will get text from text area
JOptionPane.showMessageDialog(frame,"JTextArea text is : "+textArea.getText());
}
}

public static void main(String[]args)
{
GetJTextAreaText gjbt=new GetJTextAreaText();
}
}


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