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 3 of 3

Thread: Scrollbars on JTextArea

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Scrollbars on JTextArea

    I am trying to add a vertical and horizontal scroll bar to a text area though its not working.
    JTextArea notices = new JTextArea("");
        	  JScrollPane pane = new JScrollPane(notices, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        	  notices.setPreferredSize(new Dimension(300,300));
        	  notices.setBackground(Color.YELLOW);
        	  int index = 0;
        	  try{
        	      BufferedReader reader = new BufferedReader(new FileReader(file));
        	      String line = "";
        	      while((line=reader.readLine())!=null){
        	    	  notices.append("Notice "+ ++index +":\r\n");
        	    	  notices.append(line + "\r\n");
        	      }
        	      reader.close();
        	  }catch(Exception except){
        		  JOptionPane.showMessageDialog(mainPanel, "Sorry could not be done");
        	  }
        	  notices.setEditable(false);
        	  notices.setVisible(true);
        	  center.add(pane);

    The above code gives a scroll pane on the vertical side though it does not allow me to scroll down when the text exceeds the boundaries of the JTextArea. If I change JScrollPane settings to VERTICAL_SCROLLBAR_AS_NEEDED and HORIZONTAL_SCROLLBAR_AS_NEEDED I don't get anything at all.


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Scrollbars on JTextArea

    Quote Originally Posted by keepStriving View Post
        	  notices.setPreferredSize(new Dimension(300,300));
    Don't impose a preferred size on the JTextArea ..... at most on the JScrollPane.

    Quote Originally Posted by keepStriving View Post
        	  notices.setVisible(true);
    This is not necessary. Components (excluding top-level ones like JFrame, etc...) are visible by default.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

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

    keepStriving (December 12th, 2013)

  4. #3
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Scrollbars on JTextArea

    That sorted the problem, thank you.

Similar Threads

  1. Can't figure out JTextArea. Please Help!
    By jetset22 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 9th, 2012, 09:48 PM
  2. [SOLVED] println() for JTextArea
    By KILL3RTACO in forum AWT / Java Swing
    Replies: 4
    Last Post: November 28th, 2011, 05:15 PM
  3. JTextArea Problem
    By grimx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 10th, 2011, 06:13 PM
  4. How would you get a JTextArea to know how many lines it had?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 20th, 2011, 06:58 PM
  5. JPanel with ScrollBars
    By aussiemcgr in forum AWT / Java Swing
    Replies: 1
    Last Post: September 24th, 2010, 03:45 PM