automatischer zeilenumbruch

Status
Nicht offen für weitere Antworten.

redsektor

Neues Mitglied
Hallo,

ich bin gerade ein bisschen am verzweifeln...

und zwar möchte ich in einem JTextArea einen automatischen Zeilenumbruch realisieren. Das funktioniert auch und es wird nach max 64 Zeichen umgebrochen, und es wird auch ein Wort richtig umgebrochen. Jedoch wenn ein paar Zeichen in der darüber liegenden Zeile gelöscht werden, ist das Layout kaputt. Im Prinzip soll es die gleiche Funktionalität haben wie ein Wort Dokument. Hoffe es kann mir jemand helfen.


Code:
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 * LimitedWidthDocument
 *
 * @version $LastChangedRevision$
 */
public class LimitedWidthDocument2 extends PlainDocument
{
    private static final long serialVersionUID = 1L;
    private static final int MAX_WIDTH = 64;
    private int lines = 1;
    final char[] breakPos = "\n".toCharArray(); //$NON-NLS-1$
    final char[] linePos = "\r".toCharArray(); //$NON-NLS-1$

    @Override
    public void insertString(final int offset, final String str, final AttributeSet a) throws BadLocationException
    {
        final String text = getText(0, getContent().length());

        // get position of last break before insertion point
        int lastBreakPos = 0;
        int currBreakPos = 0;
        while(currBreakPos >= 0 && currBreakPos < offset) {
            lastBreakPos = currBreakPos;
            currBreakPos = text.indexOf("\n", lastBreakPos + 1); //$NON-NLS-1$
        }

        int currLineStartPos = 0;

        // if not the first line
        if(lastBreakPos > 0){
            currLineStartPos = lastBreakPos + 1;
        }

        final int currLineEndPos = text.indexOf("\n", lastBreakPos + 1) + 1; //$NON-NLS-1$
        String currLine = ""; //$NON-NLS-1$
        int offsetInCurrLine = offset;
        if(currLineEndPos > 0) {
            currLine = text.substring(currLineStartPos, currLineEndPos);
            offsetInCurrLine = offset - currLineStartPos;
        }

        final int i = 0, laenge = 0;
        final StringBuffer sb1 = new StringBuffer();

        sb1.append(text.substring(0));

        final String[] result1 = sb1.toString().split("\\s"); //$NON-NLS-1$

        if(offset < text.length()-1){
            final int breakPos = (text.indexOf("\n", (offset/MAX_WIDTH)*MAX_WIDTH)); //$NON-NLS-1$
            final String textNewLine = getText(breakPos, text.length());
            if(text.length() - textNewLine.length() > ((offset/MAX_WIDTH)*MAX_WIDTH)-breakPos){
                super.remove(breakPos, 2);
                super.insertString(breakPos, "\n\r", a);
            }
        }

        //insert brake at the end of a line
        if(currLine.length() > 0 && (currLine.length() + str.length()) > MAX_WIDTH){

            if(!str.equals(" ")){ //$NON-NLS-1$
                //insert a brake before the last word
                if(" ".equals(text.substring(currLineStartPos + MAX_WIDTH -2, currLineStartPos + MAX_WIDTH-1))){ //$NON-NLS-1$
                    super.insertString(offset, str.toUpperCase(), a);
                    super.insertString(currLineStartPos + MAX_WIDTH-1, "\n\r", a); //$NON-NLS-1$
                    lines++;
                }
                else{
                    int x = 0;
                    final StringBuffer sb3 = new StringBuffer();

                    sb3.append(text.substring(currLineStartPos));

                    final String[] result = sb3.toString().split("\\s"); //$NON-NLS-1$

                    while(x < result.length) {
                        x++;
                    }
                    super.insertString(offset, str.toUpperCase(), a);
                    super.insertString(currLineStartPos+MAX_WIDTH-(result[x-1].length()+1), "\n\r", a); //$NON-NLS-1$
                    lines++;
                }
            }
            else{
                // insert the new break without a space
                final int breakInsertionPos = currLineStartPos + (MAX_WIDTH - str.length());

                super.insertString(breakInsertionPos, "\n\r", a); //$NON-NLS-1$
                lines++;

            }

        }
        else{
            // no breaks needed
            super.insertString(offset, str.toUpperCase(), a);
        }

        if(currLine.length() > 0 && (currLine.length() + str.length()) < MAX_WIDTH){

            if(currLineEndPos > 0) {
                currLine = text.substring(currLineStartPos, currLineEndPos);
                offsetInCurrLine = offset - currLineStartPos;
            }
        }
    }
}
[/code]
 
Was spricht gegen

JTextArea#setLineWrap

und

JTextArea#setWrapStyleWord

? Oder müssen es genau 64 Zeichen sein?
 
Hallo,

was ich vergessen hab, ich muss manuell die \n einfügen, da der String durch nen XML Parser läuft. Und soweit ich weiss, macht Wrap keine \n in den Text sondern passt einfach nur den Text zu der Area an
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben