• The JTextArea

    JTextArea is a component used so that users can type anything just like using a notepad. You can edit the fonts including the style and sizes and also html. Below is an example of a simple JTextArea.

    package com.javapointers.javase;
    
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    
    /**
     *
     * @author javapointers
     */
    public class JTextAreaTest {
        
        JTextArea text;
        JFrame frame;
        
        public JTextAreaTest(){
            frame = new JFrame();
            text = new JTextArea(5,40);
            text.setLineWrap(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(text);
            frame.pack();
            frame.setTitle("JTextArea Test");
            frame.setVisible(true);
        }
        public static void main(String args[]){
            JTextAreaTest test = new JTextAreaTest();
        }
    }
    
    

    when we run our program, the output will be like below:

    Explanation:

      • text = new JTextArea(5,40);

    means to create a new instance of JTextArea with initial height of 5 and width of 40.

    • text.setLineWrap(true);

     

    we have set the line wrap to true so that when the cursor reaches the end of the area, it will automatically sets the cursor to the next line. Setting it to false will make the characters to be displayed in just one line and some text may not be displayed.

    Related Post