Avatar billede fredand Forsker
12. marts 2002 - 11:30 Der er 6 kommentarer og
2 løsninger

JTextPane wrapps lines that are too long?

Hello!

I have a JTextPane in a JScrollPane the scrollpane is created with scroll always both at vertical and horizontal.

But the prblem is that the damned JTextPane is wrapping lines when I get to the edge of the JTextPane like this window. I want it to just append it at the end so I have to scroll with the horizontalscrollbar.

I have not seen any methods where to set the wrapping feature for this Class. Does any one know how to do this?

Best regards

Fredrik
Avatar billede yeager Nybegynder
12. marts 2002 - 12:22 #1
Jeg har fundet dette clue til dig:

public class TextEditorPane
extends javax.swing.JTextPane

trivial subclass of JTextPane that supports turning off word wrap. It will be extended to support other functionality when syntax highlighting is added.
Håber at det hjælper.
Mvh 8')
Avatar billede yeager Nybegynder
12. marts 2002 - 12:24 #2
Avatar billede yeager Nybegynder
12. marts 2002 - 12:44 #3
Denne her er måske endnu bedre som direkte svar:
You can turn line wrapping on and off with setLine-Wrap(boolean). An argument of true turns line wrapping on and an argument of false turns line wrapping off. By default, line wrapping is turned off in JTextArea.
Avatar billede fredand Forsker
13. marts 2002 - 10:19 #4
Hello Yeager!

Thanks for your answer. The TextEditorPane was intresting but I guess that can't get the java-file for it just the class file. I guess that I willl need the java-file. And I also think that I really need to use JTextPane instead of JTextArea so I guess that I can't use setLineWrap in that class.

But thanks for your time.

Best regards

Fredrik
Avatar billede yeager Nybegynder
13. marts 2002 - 10:24 #5
Well you are welcome - not much to thank me for though :o)
Avatar billede carstenknudsen Nybegynder
13. marts 2002 - 11:23 #6
Her er kildeteksten til det du har brug for, du
kan evt modificere det til din brug.
/*
*  Copyright (c) 2000, Jacob Smullyan.
*
*  This is part of SkunkDAV, a WebDAV client.  See http://skunkdav.sourceforge.net/
*  for the latest version.
*
*  SkunkDAV is free software; you can redistribute it and/or
*  modify it under the terms of the GNU General Public License as published
*  by the Free Software Foundation; either version 2, or (at your option)
*  any later version.
*
*  SkunkDAV is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with SkunkDAV; see the file COPYING.  If not, write to the Free
*  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
*  02111-1307, USA.
*/

package org.skunk.swing.text;

import java.awt.Component;
import java.awt.Dimension;
import javax.swing.plaf.ComponentUI;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;

/**
*  trivial subclass of JTextPane that supports turning off word wrap.
*  It will be extended to support other functionality when syntax
*  highlighting is added.
*/
public class TextEditorPane extends JTextPane
{
    private boolean wrap=false;

    /**
    * constructs a TextEditorPane with a default document.
    */
    public TextEditorPane()
    {
        super();
    }
  /**
    * constructs a TextEditorPane with the specified document.
    * @param doc the document
    */
    public TextEditorPane(StyledDocument doc)
    {
        super(doc);
    }

    /**
    * sets word wrap on or off.
    * @param wrap whether the text editor pane should wrap or not
    */
    public void setWordWrap(boolean wrap)
    {
        this.wrap=wrap;
    }
  /**
    * returns whether the editor wraps text.
    * @return the value of the word wrap property
    */
    public boolean getWordWrap()
    {
        return this.wrap;
    }

    public boolean getScrollableTracksViewportWidth()
    {
        if (!wrap)
        {
            Component parent=this.getParent();
            ComponentUI ui=this.getUI();
            int uiWidth=ui.getPreferredSize(this).width;
            int parentWidth=parent.getSize().width;
            boolean bool= (parent !=null)
                ? (ui.getPreferredSize(this).width < parent.getSize().width)
                : true;

            return bool;
        }
        else return super.getScrollableTracksViewportWidth();
    }
public void setBounds(int x, int y, int width, int height)
    {
        if (wrap)
            super.setBounds(x, y, width, height);
        else
        {
            Dimension size = this.getPreferredSize();
            super.setBounds(x,y,Math.max(size.width, width),Math.max(size.height, height));
        }
    }
}

/* $Log: TextEditorPane.java,v $
/* Revision 1.4  2001/01/04 06:02:49  smulloni
/* added more javadoc documentation.
/*
/* Revision 1.3  2000/12/27 22:05:09  smulloni
/* work on syntax highlighting.
/* */
Avatar billede fredand Forsker
25. marts 2002 - 12:22 #7
Thanks for your help amigos!

I have not been online for a while!!

Thanks again, you solved my problems!!

Fredrik
Avatar billede fredand Forsker
26. marts 2002 - 09:17 #8
Seems that I have not give you your point!!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester