0

So, I have a JTextArea.

I need it to be setup in a way that it prevents user from entering more than 4 rows of text. I found a way to count lines. But copy/paste has to be taken into account too. And I am not using monospaced font.

Is there a way of doing that taken all this into account?

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94

2 Answers2

4

why not add a DocumentListener and check the amount of lines each time text is removed, inserted or changed in the JTextArea:

JTextArea.getDocument().addDocumentListener(new DocumentListener() {
  public void changedUpdate(DocumentEvent e) {
    check();
  }
  public void removeUpdate(DocumentEvent e) {
    check();
  }
  public void insertUpdate(DocumentEvent e) {
    check();
  }

  public void check() {
     if (JTextArea.getLineCount()>4){//make sure no more than 4 lines
       JOptionPane.showMessageDialog(null, "Error: Cant have more than 4 lines", JOptionPane.ERROR_MESSAGE);
     }
  }
});
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Would it be possible to access the JTextArea over the DocumentEvent e? – Karlovsky120 Aug 27 '12 at 14:40
  • @Karlovsky120 what do you mean exactly? – David Kroukamp Aug 27 '12 at 14:41
  • JTextArea is a class in my program. I cannot make a reference to the class within itself, so I would have to access it as the source of DocumentEvent e somehow... Is that possible? – Karlovsky120 Aug 27 '12 at 14:44
  • use [`e.getDocument()`](http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/event/DocumentEvent.html#getDocument()) if i understand you. and to see methods of the Document see: http://docs.oracle.com/javase/1.4.2/docs/api/org/w3c/dom/Document.html – David Kroukamp Aug 27 '12 at 14:53
  • Check this tutorial it should help: http://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html – David Kroukamp Aug 27 '12 at 15:06
0

you need to define a key listener and inside that surely we need to define implemented methods , the next code is my solution I hope it helps.

////
textfield.addKeyListener(new KeyListener() {
                public void keyPressed(KeyEvent e) {
                    // TODO Auto-generated method stub
                    if(textfield.getLineCount() == maximum_number_of_your_default) {
                        c = ta.getCaret(); 
    // c is an object of Caret class as : Caret c; initialization only.
                        a = c.getDot();
    // a is an integer value initialized by zero as : int a = 0;
                    }
                    if(ta.getLineCount() > maximum_number_of_your_default){
                        c.moveDot(a);// To retrieve the caret to the last row.
                    }
    // to show line segment on the output with each enter-press key :
                    if(e.getExtendedKeyCode() == KeyEvent.VK_ENTER)
                        System.out.println("!!!!!" + ta.getLineCount() + "  " 
                        + ta.getText());    
                }
    // default methods of KeyListener class
                public void keyReleased(KeyEvent arg0) {
                    // TODO Auto-generated method stub

                }

                public void keyTyped(KeyEvent arg0) {
                    // TODO Auto-generated method stub

                }
            });
////

It my idea I hope it is correct , good luck, one world , one god , one solution for each .

Mr.MA
  • 1