Swing components for displaying text - JTextArea Tutorial

Swing provides components such as JTextArea , JEditorPane and JTextPane for displaying the simple or styled text.
Lets look at , how these classes are used to display text in java.

JTextArea :

JTextArea inherited from javax.swing.text.JTextComponent , is used to display multiline text. The preferred size is maintained by its properties like rows and columns. Now lets see how it is being used :

        JFrame frame = new JFrame("JTextArea example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flowLayout = new FlowLayout();
        frame.getContentPane().setLayout(flowLayout);

        JTextArea txtArea = new JTextArea("JTextArea example ", 10,34);

        frame.getContentPane().add(txtArea);
        
        frame.getContentPane().setComponentOrientation(ComponentOrientation.UNKNOWN);

        frame.setSize(300,400);
        frame.pack();
        frame.setVisible(true);


Output :





Now lets see the important methods for

1: Getting Text

[note]
txtArea.getText();
This method returns all the text present in the JTextArea.

txtArea.getLineCount();
This method returns number of lines in the JTextArea.

txtArea.getLineStartOffset(line);
This method returns the character index starting with the line line

txtArea.getLineEndOffset(line);
This method returns the character index ending with the line line
[/note]


Similarly we have methods for
2. Setting the text :

[note]
txtArea.setText(txt);
Method used to set the JTextArea text.

txtArea.append(txt);
Method used to append the text to the text in JTextArea.

txtArea.insert(txt, pos);
Method used to insert the text to the specified position in JTextArea.
[/note]


Now lets see the method which are used to improve the appearance of the JTextArea.

1. setFont and setForeground

Lets see the example below which is setting appearance and font of text in JTextArea.

        String txt = "This is an example of setting the appearance like \nfont and foreground color in JTextArea";
        JTextArea txtArea = new JTextArea(txt, 10,34);
        Font font = new Font("Times New Roman", Font.BOLD, 12);
        txtArea.setFont(font);
        txtArea.setForeground(Color.MAGENTA);
        


Now lets see the output :




2. setBorder and setLineWrap

By default JTextArea doesn't provide any space between the edge of JTextArea and the text inside it , if we need to add this space we can use empty border using the setBorder method, also JTextArea provides setLineWrap method to wrap the text inside the JtextArea.

Lets look at the code snippet :
        String txt = "This is an example of creating space between the text and JTextArea edge using the border and also     wrapping this text.";
        JTextArea txtArea = new JTextArea(txt, 10,34);
        Font font = new Font("Times New Roman", Font.BOLD, 12);
        txtArea.setFont(font);
        txtArea.setLineWrap(true);
        txtArea.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
        txtArea.setForeground(Color.DARK_GRAY);


Output :



Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

1 comments:

  1. how to clear the contents of jText Area through code or methods

    ReplyDelete