This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Game Development and the fly likes Scroll Pane not showing for textArea, JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Scroll Pane not showing for textArea, JFrame" Watch "Scroll Pane not showing for textArea, JFrame" New topic
Author

Scroll Pane not showing for textArea, JFrame

Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Some more GUI for my text based game. It's obviously going to need to display text properly and that means it's going to have to scroll.

This is a SSCCE of the code i'm using.
Commenting out line 35 shows the area but changing the size on line 32 does nothing. Pretty stumped right now.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8



In general
1) Never use setLayout(null) or setBounds() Always use a proper layout manager and it will figure out the bounds for you
2) Create a panel instance. Set layout to BorderLayout (panels default to FlowLayout) Create JTextArea. set word wrap etc. Create JScrollPane instance while passing Pass JTextArea instance in the constructor. Add scrollpane instance to BorderLayout.CENTER


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
I'm using setLayout(null) because I couldn't get the full version of this frame to work properly with a layout manager. I set the layout manager to null and then manually added all the labels and panels to the frame. That's why I've used setBounds, instead of setSize. So i'm stuck with layout null.

So, I should create another panel which just the textarea and scroll pane will sit in? And set the layout for that to Border?
Then eg: panel.add(textArea); and panel.add(scrollPane);?

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

Richard Henderson wrote:I'm using setLayout(null) because I couldn't get the full version of this frame to work properly with a layout manager. I set the layout manager to null and then manually added all the labels and panels to the frame. That's why I've used setBounds, instead of setSize. So i'm stuck with layout null.

In that case the correct thing to do is fix the layout problem. Null layout will always give you infinite grief

Richard Henderson wrote:
So, I should create another panel which just the textarea and scroll pane will sit in? And set the layout for that to Border?
Then eg: panel.add(textArea); and panel.add(scrollPane);?

No. Its nested children.

Notice the correct use of layout manager on line 1.

Now you can add the parent to whatever you want (even set it as the frame's content pane if you wish to)
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
OK, so I've figured out how to add the parent panel, set its bounds so it's positioned in the main window which has Layout null, then add everything else in sequence.

But now it's opaque and I can't see my background through it. parent.setOpaque(false); does not work.

Is it possible to make it transparent without changing my whole GUIWindow to BorderLayout? Because if I have to change that layout then I have 14+ labels whos positioning will be messed up.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

But now it's opaque and I can't see my background through it. parent.setOpaque(false); does not work.

Hint: JPanel has a JScrollPane which has a JTextArea
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
OK, so I do something like this:

panel parent = new panel()
textarea output = new textarea()
scrollpane scroll = new scrollpane(output)
parent.add(scroll);
output.setOpaque(false); or scroll.setOpaque(false)?

I've tried setting all three to opaque(false) and in different orders. No dice.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

All three? Why? Who is painted red? setOpaque(false) means make it transparent. Can a red wall be red if it is transparent?
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
The red background is another panel which is set as the content pane. It's not transparent.

The parent is a second panel holding only the scroll pane and text area.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

My bad. You also need a call to scrollPane.getViewport().setOpaque(false);

Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Thanks Maneesh. I would not have been able to figure out that method call.

Any chance you know a method to set the text to centered, off the top of your head? I've tried .setAlignment. Maybe it needs to be called on another method too? textArea.something().setAlignment()?

I also need to get the cursor to move to the end of the text area, to show the most recent text. Should I make a new thread for that?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

To move the cursor user setCaretPosition(int position)
For styling (such as text alignment etc) use JTextPane instead
Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Thanks. I'll check out the API for that tomorrow.
 
 
subject: Scroll Pane not showing for textArea, JFrame
 
Similar Threads
Text File to ArrayList for JTextArea?
How to copy from 4 JTextFields to 1 JTextArea
Multiline Table (2)
Background image for text based RPG
using GroupLayout