| | |
Java Text Editor (Scroll Bars)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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):
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.
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)
JTextArea t; JFrame f; JPanel c; JScrollPane b; b = new JScrollPane(); b.setAutoscrolls(true); f = new JFrame("JNotepad"); f.setBounds(300, 200, 500, 500); c = new JPanel(); f.setContentPane(c); t = new JTextArea(); t.setFont(Font.decode("Courier New-14")); b.add(t); c.add(b); f.setJMenuBar(/* Menu bar is "m". This works just fine. */m); f.setVisible(true);
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.
Geek and a Half Blog
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
check Java API here
Java Syntax (Toggle Plain Text)
JTextArea t; JFrame f; JPanel c; JScrollPane b; //b = new JScrollPane(); b.setAutoscrolls(true); f = new JFrame("JNotepad"); f.setBounds(300, 200, 500, 500); c = new JPanel(); f.setContentPane(c); t = new JTextArea(); t.setFont(Font.decode("Courier New-14")); b = new JScrollPane(t); c.add(b); f.setJMenuBar(/* Menu bar is "m". This works just fine. */m); 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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Sorry, that didn't work. In fact, nothing changed about the program. Any other ideas?
Geek and a Half Blog
![]() |
Similar Threads
- asp .net & vb .net html rich text editor (ASP.NET)
- Re: Scroll Bars for the [Code][/Code] Tagged Portion (JavaScript / DHTML / AJAX)
- undo - redo function in a text editor (Java)
- Hotmail Rich Text Editor (Windows NT / 2000 / XP)
- Best Java Program Editor? (Java)
- edlin text editor (C++)
Other Threads in the Java Forum
- Previous Thread: Itzi Bitzi Mitzi
- Next Thread: Problem in installing JOnASJOnAs 4.8.4-tomcat5.5.17
| Thread Tools | Search this Thread |
add android api applet application applications array arrays automation bank binary bluetooth chat class classes clear client code codesnippet component converter database development dice digit eclipse equation error event exception formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jni jpanel julia linux list loop main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting spamblocker sql sqlserver state storm string superclass swing text-file thread threads tree windows






