1

I have one question, which pops up during coding: I want to make sure, about this question, so I hope you could help me!

So, I'm thinking about that, is the JTextArea length infinite? Or how many chars can be used max?

I tried to write it manual, but I got bored, about 5000 lines, and 100 000 chars, so what's the limit on the JTextArea?

I'm working on a chat program, and this is important for me, but I've nowhere found the answer.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Márk Tóth
  • 11
  • 1
  • 3
  • Checkout similar question like yours https://stackoverflow.com/questions/13863795/enforce-max-characters-on-swing-jtextarea-with-a-few-curve-balls –  Oct 14 '17 at 22:30
  • 1
    I would suggest that the length is likely fixed to the maximum length of a `String` or array, which is less then `Integer.MAX_VALUE` – MadProgrammer Oct 14 '17 at 22:32
  • @SamDev I don't think the OP wants to limit the number of characters, but wants to know the theoretical maximum number of characters a `JTextArea` could hold – MadProgrammer Oct 14 '17 at 22:33
  • @MadProgrammer Can please look the header text `Length of Java Swing JTextArea?` , What does It means, Correct me. –  Oct 14 '17 at 22:36
  • 1
    @SamDev *"So, I'm thinking about that, is the JTextArea length infinite? Or how many chars can be used max?"* would suggest to me they are interested in knowing the maximum number of characters a `JTextArea` can hold - because of the internal make up of `Document`, I'd think it's bound to `Integer.MAX_VALUE` – MadProgrammer Oct 14 '17 at 22:40
  • @MadProgrammer Your Infinite may bigger then my Infinite But Here program scope defined for with some specific propose not for like mad programmer... –  Oct 14 '17 at 22:52

3 Answers3

4

So, I'm thinking about that, is the JTextArea length infinite? Or how many chars can be used max?

No, JTextArea is not infinite.

We can imply the maximum length based on the fact that JTextArea only returns a String, which has a length which returns a int. This implies that the maximum length of a JTextArea is bound to Integer.MAX_VALUE, but, because of array overheads, is slightly smaller. But in practice, you'll probably find that it's much smaller, due to the need for arrays to be laid in memory in a continuous manner, so it will depend on how much memory the JVM has available and how fragmented it is.

We can further investigate this and have a look at PlainDocument, which is the default Document used by JTextArea, which uses a char[] as it's internal data structure, just like String.

This further concretes the reasoning that the limit of a JTextArea is limited to less then Integer.MAX_VALUE

You can have a look at Do Java arrays have a maximum size?, Why I can't create an array with large size? and Why the maximum array size of ArrayList is Integer.MAX_VALUE - 8? for discussions on why an array can't be declared as Integer.MAX_VALUE

Now, before someone suggests that you could write a linked list implementation of a Document, don't forget that both Document and JTextArea rely on String, which is a key limiting factor

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

I'm working on a chat program, and this is important for me

The text area supports at least several bibles worth of text (i.e. 'a lot'). Far more than could ever be read by a casual reader and immensely more than should appear in a 'chat program'.

Here is a small example that shows more than 1.1 million lines of output on the names of Unicode characters:

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class HowLongTextArea {

    private JComponent ui = null;

    HowLongTextArea() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        JTextArea ta = new JTextArea(15, 40);
        StringBuilder sb = new StringBuilder();
        String eol = System.getProperty("line.separator");
        for (int ii=0; ii<Character.MAX_CODE_POINT; ii++) {
            sb.append((ii+1) + "\t" + Character.getName(ii) + eol);
            if (ii%10000==0) {
                System.out.println("ii: " + ii);
            }
        }
        ta.setText(sb.toString());
        ui.add(new JScrollPane(ta));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                HowLongTextArea o = new HowLongTextArea();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Thanks MadProgrammer for correcting me.

If you would like to set limit of JTextArea:

To implement a document filter, create a subclass of DocumentFilter and then attach it to a document using the setDocumentFilter method defined in the AbstractDocument class. Although it is possible to have documents that do not descend from AbstractDocument, by default Swing text components use AbstractDocument subclasses for their documents.

The TextComponentDemo application has a document filter, DocumentSizeFilter, that limits the number of characters that the text pane can contain. Here is the code that creates the filter and attaches it to the text pane's document:

JTextPane textPane;
AbstractDocument doc;
static final int MAX_CHARACTERS = 300;
...
textPane = new JTextPane();
...
StyledDocument styledDoc = textPane.getStyledDocument();
if (styledDoc instanceof AbstractDocument) {
doc = (AbstractDocument)styledDoc;
doc.setDocumentFilter(new DocumentSizeFilter(MAX_CHARACTERS));
} 

source:http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter

Jurgiele
  • 95
  • 1
  • 10
  • The OP isn't trying to limit the number of characters, but wants to know what the maximum limit IS - how many characters could a `JTextArea` possible support – MadProgrammer Oct 14 '17 at 22:53
  • *"JTextArea is infinite"* - No it's not. Based on the implementation of `PlainDocument`, the underlying storage mechanism is a `char[]`, which is limited to be less then `Integer.MAX_VALUE`, an in most cases, a lot smaller due to the need for the array elements to be laid in memory in continuous manner. Because `JTextArea` is also capable of return a `String` of the text, we can further imply that the limit of the component must be the maximum length of an `int` as `String#length` only returns a `int` ... all of which is the basic answer – MadProgrammer Oct 14 '17 at 22:59