Java Text Editor (Scroll Bars)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2005
Posts: 45
Reputation: Sp!ke is an unknown quantity at this point 
Solved Threads: 0
Sp!ke's Avatar
Sp!ke Sp!ke is offline Offline
Light Poster

Java Text Editor (Scroll Bars)

 
0
  #1
Jan 24th, 2007
I'm learning Java right now, and one of the projects I've been working on as practice lately is just a simple text editor (using Swing). The one thing, though, that I can't for the life of me figure out, is how to add scroll bars to the text box if the text overflows.

Here's some of my code (There's way more, I just simplified what I have right now that makes the frame and text box):

  1. JTextArea t;
  2. JFrame f;
  3. JPanel c;
  4. JScrollPane b;
  5. b = new JScrollPane();
  6. b.setAutoscrolls(true);
  7. f = new JFrame("JNotepad");
  8. f.setBounds(300, 200, 500, 500);
  9. c = new JPanel();
  10. f.setContentPane(c);
  11. t = new JTextArea();
  12. t.setFont(Font.decode("Courier New-14"));
  13. b.add(t);
  14. c.add(b);
  15. f.setJMenuBar(/* Menu bar is "m". This works just fine. */m);
  16. f.setVisible(true);
A little sloppy, I know, but I had to copy and paste and reorganize it from the rest of my code. I can honestly say I don't even have a remote idea as to how you add scroll bars, what's there is just a guess. The notepad works, but the scrollbars don't.

I also have an anaonymous inner class designed to resize the textbox to the size of the form if the mouse is moved over it, because I didn't know how else to do it (there was no "onResizeForm" event, was there?), so if that's interfering, well, let me know and I'll figure something out.
Last edited by Sp!ke; Jan 24th, 2007 at 10:28 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,197
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Java Text Editor (Scroll Bars)

 
0
  #2
Jan 24th, 2007
There is no add method for JScrollPane so you can't use it but there is JScrollPane constructor JScrollPane(Component view), which "Creates a JScrollPane that displays the contents of the specified component, where both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view." (taken fro JAVA API)
check Java API here
  1. JTextArea t;
  2. JFrame f;
  3. JPanel c;
  4. JScrollPane b;
  5. //b = new JScrollPane();
  6. b.setAutoscrolls(true);
  7. f = new JFrame("JNotepad");
  8. f.setBounds(300, 200, 500, 500);
  9. c = new JPanel();
  10. f.setContentPane(c);
  11. t = new JTextArea();
  12. t.setFont(Font.decode("Courier New-14"));
  13. b = new JScrollPane(t);
  14. c.add(b);
  15. f.setJMenuBar(/* Menu bar is "m". This works just fine. */m);
  16. f.setVisible(true);
Last edited by peter_budo; Jan 24th, 2007 at 12:11 pm.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 45
Reputation: Sp!ke is an unknown quantity at this point 
Solved Threads: 0
Sp!ke's Avatar
Sp!ke Sp!ke is offline Offline
Light Poster

Re: Java Text Editor (Scroll Bars)

 
0
  #3
Jan 25th, 2007
Sorry, that didn't work. In fact, nothing changed about the program. Any other ideas?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Java Text Editor (Scroll Bars)

 
0
  #4
Jan 25th, 2007
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC