Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By Fubarable

Thread: text area help

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default text area help

    i have a text area, and whenever text is typed in it, it automatically resizes. here is my code:

    Java Code:
    areaScrollPane = new JScrollPane(textArea);
    		areaScrollPane.setVerticalScrollBarPolicy(
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		areaScrollPane.setPreferredSize(new Dimension(250, 250));
    
    textArea = new JTextArea(5, 20);
    		textArea.setEditable(false);
    		textArea.setLineWrap(true);
    		textArea.setWrapStyleWord(true);
    textArea.setMargin(new Insets(10, 10, 10, 10));

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: text area help

    Is that what you want it to do?
    Fubarable likes this.

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

    Default Re: text area help

    Consider asking a well phrased question.

  4. #4
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: text area help

    sorry. basically it will resize if the content goes out of the text area's bounds. i would rather it show the scroll bars.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: text area help

    What is the value of the textArea variable on line 1.
    It's changed on line 6.

    Can you post a SSCCE to show the problem.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: text area help

    Moved from 'New to Java'

    I would expect someone who's been a member for nearly a year and has more than 200 posts to be able to post in the correct section of the forums.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: text area help

    Java Code:
    	JTextArea textArea;
    	JButton jbtnSubmit;
    	JScrollPane areaScrollPane;
    
    	public static void main(String[] args) {
    		test gui = new test();
    		gui.startGUI();
    	}
    
    	// Set up the GUI end for the user
    	public void startGUI() {
    		// These are all essential GUI pieces
    		new JTextArea("");
    
    		areaScrollPane = new JScrollPane(textArea);
    		areaScrollPane
    				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		areaScrollPane.setPreferredSize(new Dimension(250, 250));
    
    		final JFrame jfrm = new JFrame("my program");
    		jfrm.setLayout(new FlowLayout());
    		jfrm.setSize(300, 300);
    		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		textArea = new JTextArea(5, 20);
    		textArea.setEditable(false);
    		textArea.setLineWrap(true);
    		textArea.setWrapStyleWord(true);
    		textArea.setText("hi   \n\n\n\n how are you?\n\n\n\n"); // <<< continue to add text 
                    //here, and see how big the text area grows
    
    		jbtnSubmit = new JButton("Submit");
    		textArea.setMargin(new Insets(10, 10, 10, 10));
    
    		jfrm.add(textArea);
    		jfrm.add(jbtnSubmit);
    		jfrm.setVisible(true);
    	}

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

    Default Re: text area help

    Please only post real code that you've used. Your current code will not compile.

    Once you get it to compile, you'll need to fix some careless errors. Consider creating the JTextArea before you add it to the JScrollPane. Consider actually adding the JScrollPane then to the GUI rather than the JTextArea. These errors are only occurring due to carelessness so the most important bit of advice is that you plan your program before you type it so that you avoid sloppy code.
    Norm likes this.

  9. #9
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: text area help

    thanks! seems to work now. the last thing that i would like to see is that when text is added, and it goes out of scope of the viewing range, that it auto scrolls to the bottom, rather than staying stagnant.

Similar Threads

  1. adding text to a text area dynamically
    By tom2zip in forum AWT / Swing
    Replies: 3
    Last Post: 01-17-2012, 06:58 AM
  2. Replies: 8
    Last Post: 07-17-2011, 03:38 PM
  3. Replies: 0
    Last Post: 12-09-2010, 06:56 PM
  4. Replies: 0
    Last Post: 09-28-2010, 10:52 AM
  5. eol in text area....
    By Nicholas Jordan in forum Networking
    Replies: 0
    Last Post: 09-14-2008, 11:59 PM

Posting Permissions

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