How to limit the number of lines in JTextArea?

I am trying to create a graphical interface for a service that has a JTextArea for viewing messages, each message is written on one line and, if necessary, is put off a word.

The messages come through the socket, so this is just the .append (message) message that I use to update JTextArea, I need to limit these lines to 50 or 100, and I do not need to limit the number of characters on each line.

Is there a method for restricting numeric strings in JTextArea, or is there an alternative way to do it?

I could really use the help on this.

Edit

The problem is that every client can send endless lines, all these lines should be readable, so this is not a simple check of the number of lines in JTextArea. I need to delete old lines in order to view newer lines.

+7
source share
4 answers

Would it be more efficient?

final int SCROLL_BUFFER_SIZE = 100; public void trunkTextArea(JTextArea txtWin) { int numLinesToTrunk = txtWin.getLineCount() - SCROLL_BUFFER_SIZE; if(numLinesToTrunk > 0) { try { int posOfLastLineToTrunk = txtWin.getLineEndOffset(numLinesToTrunk - 1); txtWin.replaceRange("",0,posOfLastLineToTrunk); } catch (BadLocationException ex) { ex.printStackTrace(); } } } 
+6
source

Use this to get row and column

Add a DocumentFilter that checks the number of lines (pass the offset doc.getLength ()) and don't add more text.

Or you can create a dummy invisible JTextArea and add all the text there. Then measure the last line allowed and cut the text.

+6
source

Below is a rough DocumentFilter that seems to work. His basic approach is to allow you to insert / add an event, request the number of rows after the fact, if it is greater than max, delete the rows from the very beginning, if necessary.

Beware: lines counted using textArea methods are (most likely, awaiting confirmation from @Stani) strings-between-cr, not the actual strings as layout. Depending on your exact requirement, they may or may not group you (if not, use the Stan utility methods)

I was surprised and not quite sure if he was safe.

  • surprised: the insert method is not called, which is necessary to implement the replacement method instead (in the finished ready-made code, both are possible)
  • I'm not sure that in text mode the guaranteed values ​​will be returned in the filtering methods (maybe not, then the length check can be completed in invokeLater).

Some codes:

 public class MyDocumentFilter extends DocumentFilter { private JTextArea area; private int max; public MyDocumentFilter(JTextArea area, int max) { this.area = area; this.max = max; } @Override public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { super.replace(fb, offset, length, text, attrs); int lines = area.getLineCount(); if (lines > max) { int linesToRemove = lines - max -1; int lengthToRemove = area.getLineStartOffset(linesToRemove); remove(fb, 0, lengthToRemove); } } } // usage JTextArea area = new JTextArea(10, 10); ((AbstractDocument) area.getDocument()).setDocumentFilter(new MyDocumentFilter(area, 3)); 
+6
source

My approach works with the contents of JTextPane as a document. It is assumed that your program adds text to the end of the document many times, so when you call doc.getCharacterElement (1), you will get basically a leaf element, which is the earliest line of code. This is what we want to remove, so we get the start and end offsets of this element and call the remove method to cut out the earliest text.

This can be done selectively by checking to see if the number of characters exceeds more than a million (as I did below), or based on the number of lines you have already added.

If you want to base it on the number of lines already added, you will need a field that increases every time you print on JTextPane, so this code runs once every time you print a new line of text, and the count exceeds your maximum value.

  JTextPane pane = ... StyledDocument doc = pane.getStyledDocument(); int size = doc.getLength(); if (size > 1000000) { out.println("Size: " + size); out.println(":" + 1); int i = 0; Element e = doc.getCharacterElement(i + 1); int start = e.getStartOffset(); int end = e.getEndOffset(); try { doc.remove(start, end); } catch (BadLocationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } 

Hooray!

+1
source

All Articles