JTextArea automatic line break

Hello everybody, I have to implement a JTextArea with a fixed rowlength of 50 characters. After the 50th character a line break ("\n") should automatically be inserted. I tried to fix my problem using the method insertString(int offs, String str, AttributeSet attribute) of the DefaultStyledDocument. See code example! The problem occurs if the user pastes more than 50 characters into the textarea. Anybody an idea to fix my problem?


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;


public class TestEditor extends JFrame implements DocumentListener {

	private static final long serialVersionUID = 7840185264770309196L;
	private JTextArea textArea;	
	private JTextField txt_number_off_characters;
	
	private TestEditor() {
		super("Test");
		setDefaultCloseOperation(EXIT_ON_CLOSE);		
		textArea = new JTextArea();
		Font f = new Font("Courier", Font.PLAIN, 12);
		textArea.setFont(f);
		LimitedRowLengthDocument myDoc = new LimitedRowLengthDocument(textArea, 50);		
		textArea.setDocument(myDoc);
		myDoc.addDocumentListener(this);		
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		txt_number_off_characters = new JTextField();
				
		BorderLayout bl = new BorderLayout();
		this.setLayout(bl);
		
		this.setMinimumSize(new Dimension(400, 250));
		this.setPreferredSize(new Dimension(400, 250));
		
		add(txt_number_off_characters, BorderLayout.SOUTH);
		add(new JScrollPane(textArea), BorderLayout.CENTER);
		
		pack();
		setVisible(true);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new TestEditor();
	}


	public void changedUpdate(DocumentEvent e) {
	}


	public void insertUpdate(DocumentEvent e) {		
		final Document doc = e.getDocument();
		txt_number_off_characters.setText("Textlength: " + String.valueOf(doc.getLength()));		
	}


	public void removeUpdate(DocumentEvent e) {
		Document doc = e.getDocument();
		txt_number_off_characters.setText("Textlength: " + String.valueOf(doc.getLength()));
	}

}



import javax.swing.JTextArea;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;

public class LimitedRowLengthDocument extends DefaultStyledDocument {

	private static final long serialVersionUID = -2059737037274062490L;

    private static final String EOL = "\n";
	
    private int max;
    private JTextArea ta = null;

    public LimitedRowLengthDocument(JTextArea ta, int max) {
    	this.ta = ta;
    	this.max = max;
    }

    public void insertString(int offs, String str, AttributeSet attribute) throws BadLocationException { 
    	
    	int actRow = ta.getLineOfOffset(offs);
    	int rowBeginn = ta.getLineStartOffset(actRow);
    	int rowEnd = ta.getLineEndOffset(actRow);
      	int referenceValue = 0;
      	
    	if (str.length() > 1) {
    		referenceValue = (rowEnd + str.length()) - rowBeginn;
    	} else {
    		referenceValue = rowEnd - rowBeginn;
    	}
    	
    	if (referenceValue >= max) {
        	if (str.length() > 1) {        		
        		StringBuffer str_buff = new StringBuffer();
        		for (int i=0; i<str.length(); i++) {
        			if (i >= max) {
        				str_buff.append(EOL);
        				str_buff.append(str.charAt(i));
        				str = str.substring(i, str.length());
        				i = 0;
        			} else {
        				str_buff.append(str.charAt(i));
        			}
        		}        		
        		str = str_buff + EOL;
        	} else {
        		str = EOL + str;
        	}    		
    	}    	
    	super.insertString(offs, str, attribute);
    }
}

Thank you very much... Greeting, Micha

Join the discussion
Be the first to comment on this article. Our Commenting Policies