Results 1 to 13 of 13
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default [SOLVED] Creating an auto-expanding JTextArea

    I'm wondering how you could create a JTextArea that expands as the number of lines increases. Is this at all possible?

    Hopefully I'm not missing something obvious...

    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    what do you mean by "expand"?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I want the JTextArea's height to increase whenever the user begins typing on a new line (Even if they do not press enter - which is what is causing the difficulty - A KeyListener would solve it if I only wanted it to expand when enter was pressed.)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you can use JTextArea.getLineCount() to track the enter keys and increase height.

    For tracking w/o pressing enter key, assuming you have setLineWrap to true, I'm thinking maybe you can use JTextArea.getDocument().getLength() to track it.
    Java Code:
          if((textArea.getDocument().getLength() > x)){
            textArea.setBounds(20,20,100,x+=20);
          }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Document.getLength() returns characters, so I doubt it will do what I want efficiently. Does JTextArea.getText() return the newline characters as well? (not only the ones created by pressing enter, but also the ones created because of the setLineWrap being true) That would probably help me with what I want with a bit of work, but I can't find any information on it.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One way is to use a document listener
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    
    public class ExpandingTextArea {
      private static final int MIN_ROWS = 10;
      private static final int MIN_COLS = 20;
      private JPanel mainPanel = new JPanel();
      private PlainDocument doc = new PlainDocument();
      private JTextArea textArea = new JTextArea(doc);
      private Dimension minTextAreaSize;
    
      public ExpandingTextArea() {
        textArea.setRows(MIN_ROWS);
        textArea.setColumns(MIN_COLS);
        minTextAreaSize = textArea.getPreferredSize();
        textArea.setMinimumSize(textArea.getPreferredSize());
        mainPanel.add(textArea);
    
        doc.addDocumentListener(new DocumentListener() {
    
          public void changedUpdate(DocumentEvent e) {
    	checkAndSetSize();
          }
    
          public void insertUpdate(DocumentEvent e) {
    	checkAndSetSize();
          }
    
          public void removeUpdate(DocumentEvent e) {
    	checkAndSetSize();
          }
        });
      }
    
      private void checkAndSetSize() {
        if (!(textArea.getSize().equals(textArea.getPreferredSize()))) {
          Window win = SwingUtilities.getWindowAncestor(mainPanel);
          win.pack();
          win.setLocationRelativeTo(null);
        }
      }
    
      public JComponent getComponent() {
        return mainPanel;
      }
    
      private static void createAndShowUI() {
        JFrame frame = new JFrame("ExpandingTextArea");
        frame.getContentPane().add(new ExpandingTextArea().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
    	createAndShowUI();
          }
        });
      }
    }

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    @Fubarable: Your code just expands the JFrame, not the actual JTextArea itself, as far as I can tell... I need the JTextArea to add rows whenever it wraps (or user presses enter)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    getDocument().getLength() returns an int.

    and getText().length() would both work, I believe.
    But you'll have to do some math to calculate the col and increase the height when the length exceeds col width.

    I did a test w/ getDocument().getLength() and works, but don't have the exact math worked out yet.

    I think getText would be slower since it creates a new string and discards it everytime.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... good point about max column width. Problem is I have it wrapping at whitespaces, instead of at characters. I could change that, but it would be good if I didn't have to. (It's supposed to display messages for the user, and wrapping at characters would just make it look strange...)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    I did say use with getLineCount() in the past posts....

    But none of that matters, because
    Fubarable's code works. just add in
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);

    and
    Java Code:
    if (!(textArea.getSize().equals(textArea.getPreferredSize()))) {
          textArea.setSize(textArea.getPreferredSize());
    Last edited by angryboy; 06-10-2009 at 01:14 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class SizingTest {
        private JScrollPane getContent(int rows, int cols) {
            JTextArea textArea = new JTextArea(rows, cols) {
                public Dimension getPreferredScrollableViewportSize() {
                    return getPreferredSize();
                }
            };
            Font font = textArea.getFont();
            textArea.setFont(font.deriveFont(18f));
            textArea.setMargin(new Insets(5,5,5,5));
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(textArea, new GridBagConstraints());
            return new JScrollPane(panel);
        }
    
        public static void main(String[] args) {
            SizingTest test = new SizingTest();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getContent(1,5));
            f.setSize(300,300);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  12. #12
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    !!That's amazing.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  13. #13
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... Fubarable's code does work... didn't have compiler earlier to test it with.

    Great code. With a few edits and some listeners it will work fine. Thanks to everyone who helped out.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. [SOLVED] JList Expanding when not all contents visible
    By Singing Boyo in forum New To Java
    Replies: 8
    Last Post: 06-01-2009, 05:29 AM
  2. [SOLVED] Prevent JTextField from Expanding Within JFrame
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 05-15-2009, 02:00 AM
  3. Collapsing/Expanding() All nodes in a TreeMenu using javascript
    By Hadley in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 01-30-2009, 11:18 PM
  4. JSP Auto Log -In
    By Eku in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 09-18-2008, 03:58 AM
  5. Auto-complete/Auto-fix for custom statement
    By dark_cybernetics in forum Eclipse
    Replies: 0
    Last Post: 08-19-2008, 11:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •