• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scroll Pane not showing for textArea, JFrame

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Richard Henderson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad. You also need a call to scrollPane.getViewport().setOpaque(false);

 
Richard Henderson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To move the cursor user setCaretPosition(int position)
For styling (such as text alignment etc) use JTextPane instead
 
Richard Henderson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I'll check out the API for that tomorrow.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic