Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Swing Code Examples

Using JTextArea

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
Using JTextArea import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TextAreaDemo extends JFrame { private JTextArea t1, t2; private JButton copy; public TextAreaDemo() { super( "TextArea Demo" ); Box b = Box.createHorizontalBox(); String s = "This is a demo string to\n" + "illustrate copying text\n" + "from one TextArea to \n" + "another TextArea using an\n"+ "external event\n"; t1 = new JTextArea( s, 10, 15 ); b.add( new JScrollPane( t1 ) ); copy = new JButton( "Copy >>>" ); copy.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { t2.setText( t1.getSelectedText() ); } } ); b.add( copy ); t2 = new JTextArea( 10, 15 ); t2.setEditable( false ); b.add( new JScrollPane( t2 ) ); Container c = getContentPane(); c.add( b ); // Box placed in BorderLayout.CENTER setSize( 425, 200 ); show(); } public static void main( String args[] ) { TextAreaDemo app = new TextAreaDemo(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }