25 Sept 2012

append new line on top of the jtextarea in java swing

/* how to insert or append new line on top of the jtextarea in java swing */
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;

public class TestTextArea {

    private void initUI() {
        JFrame frame = new JFrame("textarea example");
        final JTextArea textarea = new JTextArea(24, 80);
        JButton addText = new JButton("Add new line");
        addText.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try { 
                    textarea.getDocument().insertString(0,"New line entered      on"+new Date()+"\n",null);
                     } 

                catch (BadLocationException e1) 
                    {
                    e1.printStackTrace();
                   }
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(textarea));
        frame.add(addText, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new TestTextArea().initUI();
            }
        });
    }

}

0 comments:

Post a Comment