Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #688

Re: JTextArea contraints

From "jc_halsey" <jc_halsey@THRWHITE.remove-dii-this>
Subject Re: JTextArea contraints
Message-ID <1168886480.353851.169860@a75g2000cwd.googlegroups.com> (permalink)
Newsgroups comp.lang.java.gui
References <nGPqh.191449$gl2.42615@newsfe16.lga>
Date 2011-04-27 15:28 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
That just might work, but i am using word wrap....
does the word wrap use /n or /r/n?


Knute Johnson wrote:
> jc_halsey@yahoo.com wrote:
> > Could you explain what you mean by line feeds?
> >
> > Knute Johnson wrote:
> >> jc_halsey@yahoo.com wrote:
> >>> I have tried and cont. to limit the rows(lines) on a JTextArea.
> >>> I have created a PlainDoc. and have successfully limited the max
> >>> char(s).
> >>> The max chars is good for the text area as a whole if you start at 0
> >>> and fill every line.
> >>> However in this case, which would be a text editor, a vertical limit is
> >>> needed.
> >>>
> >>> The user can not exceed past row/line 17. As is now, the user can type
> >>> past line 17.
> >>> Any suggestions on how to limit?
> >>>
> >>> Posted is the Plain Doc., to show the limit on maxchar(s)
> >>>
> >>>
> >>> public class MaxLengthDocument extends PlainDocument {
> >>>
> >>>  private int maxRows = 0;
> >>>
> >>>   // create a Document with a specified max length
> >>>   public MaxLengthDocument(int maxLength) {
> >>>     max = maxLength;
> >>>   }
> >>>
> >>>   // don't allow an insertion to exceed the max length
> >>>   public void insertString(int offset, String str, AttributeSet a)
> >>>       throws BadLocationException {
> >>>
> >>>     if (getLength() + str.length() > max )
> >>>       java.awt.Toolkit.getDefaultToolkit().beep();
> >>>
> >>>     else
> >>>       super.insertString(offset, str, a);
> >>>   }
> >>> }
> >>>
> >> I think I would try to count line feeds in your Document.  If there are
> >> already 16 don't allow any text to be entered that contains a line feed.
> >>
> >> --
> >>
> >> Knute Johnson
> >> email s/nospam/knute/
> >
>
> Well you got me intrigued so take a look at this example, it limits the
> JTextArea to three lines of text.
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import javax.swing.text.*;
>
> public class test {
>      public static void main(String[] args) {
>          Runnable r = new Runnable() {
>              public void run() {
>                  JFrame f = new JFrame();
>                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>
>                  class LDoc extends PlainDocument {
>                      public void insertString(int offs, String str,
>                       AttributeSet a) throws BadLocationException {
>                          String text = getText(0,getLength());
>                          int lines = 0;
>                          for (int i=0; i<text.length(); i++)
>                              if (text.charAt(i) == '\n')
>                                  ++lines;
>                          System.out.println(str.indexOf('\n'));
>                          if (lines == 2 && str.indexOf('\n') >= 0)
>                              return;
>                          else
>                              super.insertString(offs, str, a);
>                      }
>                  };
>                  JTextArea ta = new JTextArea(new LDoc(),"",5,20);
>                  f.add(ta,BorderLayout.CENTER);
>                  f.pack();
>                  f.setVisible(true);
>              }
>          };
>          EventQueue.invokeLater(r);
>      }
> }
> 
> -- 
> 
> Knute Johnson
> email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

JTextArea contraints "jc_halsey" <jc_halsey@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
  Re: JTextArea contraints "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
    Re: JTextArea contraints "jc_halsey" <jc_halsey@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
      Re: JTextArea contraints "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
        Re: JTextArea contraints "jc_halsey" <jc_halsey@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
  Re: JTextArea contraints "Michael Dunn" <michael.dunn@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000

csiph-web