Best way to limit max number of characters per line in JEditorPane ?

Discussion in 'Java' started by Peter, Jan 19, 2010.

  1. Peter

    Peter Guest

    Hi,

    I need to write swing based editor using which allows the user to
    write a plain text document, which has a fixed maximum column width.
    The column width will be supplied by the model class later. I finally
    managed to write one using a document filter, as shown below. My
    question is this the best (or a reasonable) way to realize this or are
    there better options / solutions / ideas ?

    Any comments are welcome.




    Code:
    public class LMEditor extends JFrame {
    
    	private int frameWidth = 600 ;
    	private int frameHeight = 400 ;
    	private JEditorPane motivationTxtEdt = null ;
                    private static final int MAXCOLUMNS = 30 ;
    
    	public LMEditor(){
    		super() ;
    		initialize() ;
    	}
    
    
    	public void initialize() {
    
    		// Init frame
    		setDefaultCloseOperation(EXIT_ON_CLOSE) ;
    		this.setSize(frameWidth,frameHeight) ;
    
    .... irrelevant code ...
    
    		motivationTxtEdt = new JEditorPane() ;
    		motivationTxtEdt.setBounds(20,120,400,300) ;
    		motivationTxtEdt.setContentType("text/plain") ;
    		Document motivationDoc = motivationTxtEdt.getDocument();
    	                DocumentFilter motivationFilter = new
    ColumnWidthDocumentFilter(MAXCOLUMNS,motivationTxtEdt);
    	                ((AbstractDocument) motivationDoc).setDocumentFilter
    (motivationFilter);
    		LMEditorPanel.add(motivationTxtEdt) ;
    
    		 setVisible(true) ;
    
    	}
    
    	class ColumnWidthDocumentFilter extends DocumentFilter {
    
    		int maxColumnWidth = 0 ;
    		JEditorPane motivationEditorPane = null ;
    
    		public ColumnWidthDocumentFilter(int maxColumnWidth, JEditorPane
    motivationEditorPane) {
    
    			this.maxColumnWidth = maxColumnWidth ;
    			this.motivationEditorPane = motivationEditorPane ;
    
    		}
    
    		public void insertString(DocumentFilter.FilterBypass fb, int offset,
    String string,
    				AttributeSet attr) throws BadLocationException {
    			super.insertString(fb, offset, string, attr);
    		}
    
    		public void remove(DocumentFilter.FilterBypass fb, int offset, int
    length)
    				throws BadLocationException {
    		    super.remove(fb, offset, length);
      	  	}
    
    		public void replace(DocumentFilter.FilterBypass fb, int offset, int
    length, String text,
    				AttributeSet attrs) throws BadLocationException {
    
    			// Get the caret position
    		    int caretPosition = motivationEditorPane.getCaretPosition() ;
    		    // Get line and linestart of root Document
    		    Element root = fb.getDocument().getDefaultRootElement() ;
    			int line = root.getElementIndex( caretPosition );
    			int lineStart = root.getElement( line ).getStartOffset();
    
    			if (((caretPosition - lineStart + 1) < this.maxColumnWidth) ||
    (text == "\n")) {
    			    super.replace(fb, offset, length, text, attrs);
    			}
    
    		  }
    
    		}
     
    Peter, Jan 19, 2010
    #1
    1. Advertising

  2. In article
    <>,
    Peter <> wrote:

    > I need to write swing based editor, using [a component that] allows
    > the user to write a plain text document, which has a fixed maximum
    > column width. The column width will be supplied by the model class
    > later. I finally managed to write one using a document filter, as
    > shown below. My question is this the best (or a reasonable) way to
    > realize this or are there better options / solutions / ideas ?
    >
    > Any comments are welcome.


    I tend to think of width as a property of the container in which the
    text is rendered, rather than a property of the text itself. JEditorPane
    seems to have been designed accordingly. If you intended to limit the
    maximum line length, your approach seems reasonable. You might also look
    at leveraging an existing line-oriented editor such as jEdit:

    <http://www.jedit.org/>

    [Interesting approach to text wrap in JEditorPane.]

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jan 19, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,355
    Matt Humphrey
    Oct 18, 2004
  2. =?Utf-8?B?Q2hyaXNGcm9obGljaA==?=

    Max Number of Web Controls Per Page

    =?Utf-8?B?Q2hyaXNGcm9obGljaA==?=, Jul 7, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    463
    Andrew Robinson
    Jul 7, 2006
  3. ChrisFrohlich

    Max Number of Web Controls Per Page???

    ChrisFrohlich, Jul 7, 2006, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    930
    Bob Lehmann
    Jul 8, 2006
  4. Ken Fine
    Replies:
    2
    Views:
    205
    Ken Fine
    Feb 5, 2004
  5. Greg Ferris

    Textarea max rows and max characters per row

    Greg Ferris, Jan 16, 2004, in forum: Javascript
    Replies:
    2
    Views:
    612
    Greg Ferris
    Jan 16, 2004
Loading...

Share This Page