944,193 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8773
  • Java RSS
Jan 24th, 2007
0

Java Text Editor (Scroll Bars)

Expand Post »
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):

java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 14
Solved Threads: 0
Light Poster
Sp!ke is offline Offline
45 posts
since Jul 2005
Jan 24th, 2007
1

Re: Java Text Editor (Scroll Bars)

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
Java Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 875
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Jan 25th, 2007
0

Re: Java Text Editor (Scroll Bars)

Sorry, that didn't work. In fact, nothing changed about the program. Any other ideas?
Reputation Points: 14
Solved Threads: 0
Light Poster
Sp!ke is offline Offline
45 posts
since Jul 2005
Jan 25th, 2007
0

Re: Java Text Editor (Scroll Bars)

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Itzi Bitzi Mitzi
Next Thread in Java Forum Timeline: Problem in installing JOnASJOnAs 4.8.4-tomcat5.5.17





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC