/* The program creates a JTextArea with scroll bars * */ import javax.swing.*; import java.awt.event.*; public class TextArea extends JFrame { JPanel panel; JButton ok; JTextArea txt; JScrollPane scroll; public TextArea() { // Sets the title setTitle("Text Area"); // Sets the size setSize(300, 400); // Sets the event when the window close setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Creates a panel panel = new JPanel(); // Creates an empty Text Area with 10 rows and 20 columns txt = new JTextArea("", 10, 20); // Creates a button ok = new JButton("OK"); // Create a scroll pane in the text area scroll = new JScrollPane(txt,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // Adds the scroll to the panel panel.add(scroll); // Adds the button to the panel panel.add(ok); // Adds the panel to the content pane add(panel); // Sets the window to be visible setVisible(true); // Creates an action listener ok.addActionListener(new ButtonListener()); } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==ok) { // Appends Hello World to the text area whenever ok button is clicked txt.append("Hello World!!!\n"); } } } public static void main(String[] args) { new TextArea(); } }