Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Expanding JTextArea

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Expanding JTextArea

    Hey guys, I have a problem where my JTextArea is expanding when I type off the given space. Both in height and width. I've tried setMaximumSize, setSize, setColumns, setRows, and other methods. None of them seem to work.

    JTextArea textField = new JTextArea("", 15, 40);
    textField.setSize(new Dimension(15, 40)); //did this to reinforce the dimensions but it doesn't work :(

    Any help would be appreciated.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Expanding JTextArea

    Call setLineWrap(true) on the JTextArea. This will help horizontally. Vertically, place your JTextArea in a JScrollPane.

  3. The Following User Says Thank You to copeg For This Useful Post:

    The_Mexican (November 27th, 2010)

  4. #3
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Expanding JTextArea

    Thanks, that makes sense. However, now a problem with the JScrollPane...nothing is showing up. Here is my code:

    TextArea textField = new JTextArea("", 15, 40);
    textField.setLineWrap(true);
    JScrollPane scrollPane = new JScrollPane(textField);
    contentPane.add(scrollPane);

    I have no idea what's wrong.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Expanding JTextArea

    Please define 'nothing is showing up'

  6. #5
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Expanding JTextArea

    Quote Originally Posted by copeg View Post
    Please define 'nothing is showing up'
    There is no scrollbar.

    EDIT: Just to make it clear, everything else is working fine, no errors, it's just that the scrollbar isn't showing up.

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Expanding JTextArea

    It will help with a complete (and simple) example that demonstrates this behavior. Does your code set the preferred size of the JTextArea anywhere? If so remove those lines and see if that fixes the problem.

  8. #7
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Expanding JTextArea

    Quote Originally Posted by copeg View Post
    It will help with a complete (and simple) example that demonstrates this behavior. Does your code set the preferred size of the JTextArea anywhere? If so remove those lines and see if that fixes the problem.
    The only place where I define the size of the JTextArea is in the line where I initiate it:
    JTextArea textField = new JTextArea("", 15, 40);

    Removing that makes the JTextArea very small, and the JScrollPane continues to not appear.

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Expanding JTextArea

    A short, compilable example that demonstrates the problem would help tremendously.

  10. #9
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Expanding JTextArea

    Ah, missed that. Here's a little edited part of the code:

    import java.awt.Container;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SpringLayout;
    public class testing extends JPanel {
    	public static void main(String[] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGame();
                }
            });
        }
        public static void createAndShowGame() {
        	JTextArea textField = new JTextArea("", 15, 40);
        	JFrame frame = new JFrame("Frame");
        	textField.setLineWrap(true);
            JScrollPane scrollPane = new JScrollPane(textField);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container contentPane = frame.getContentPane();
            SpringLayout layout = new SpringLayout();
            contentPane.setLayout(layout);
            contentPane.add(textField);
            contentPane.add(scrollPane);
            frame.pack();
            frame.setSize(458, 325);
            frame.setVisible(true);
        }    
    }

    As you can see, when you run it, there is no Scrollbar.

  11. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Expanding JTextArea

    The posted code adds the JTextArea twice (adding itself, then the scrollpane that contains the JTextArea). Since a component can only have one parent, you will get the behavior you see. Remove the addition of the JTextArea, and just add the JScrollPane (since it already contains the JTextArea). If you want the horizontal scrollbar, remove the setLineWrap, otherwise keep it there if you want it to wrap.

  12. The Following User Says Thank You to copeg For This Useful Post:

    The_Mexican (November 27th, 2010)

  13. #11
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Expanding JTextArea

    Ah, thanks a lot!

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. JTextArea output problem
    By grimx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 8th, 2010, 07:54 PM
  3. Replies: 5
    Last Post: June 10th, 2010, 10:19 AM
  4. Expanding the capacity of an array
    By dubois.ford in forum Collections and Generics
    Replies: 5
    Last Post: May 7th, 2010, 05:58 AM
  5. Replies: 1
    Last Post: May 21st, 2009, 03:41 AM