Question

Hello I'm trying to make a game and a basic thing I want to do is add a JTextArea with a scrollbars. This is my code:

package TestStuff;

import javax.swing.*;

@SuppressWarnings("serial")
public class JTextAreaTest extends JFrame
{
     public static void main(String[] args)
     {
          new JTextAreaTest();
     }

     public JTextAreaTest()
     {
          this.setSize(1500, 600);
          setDefaultCloseOperation(
               JFrame.EXIT_ON_CLOSE);
          this.setLocation(450, 175);
          this.setExtendedState(JFrame.
MAXIMIZED_BOTH);
          this.setTitle("Game Display Test");

          panel1 = new JPanel(null);

          final JTextArea gameDisplay = new JTextArea(
               500, 300);
          gameDisplay.setBounds(424, 300, 500, 300);
          gameDisplay.setBackground(Color.BLACK);

          Font font = new Font ("Verdana", Font.BOLD, 
              14);
          gameDisplay.setFont(font);
          gameDisplay.setForeground(Color.WHITE);

          final JScrollPane displayScroll = new JScrollPane(
               gameDisplay);
          displayScroll.setHorizontalScrollBarPolicy(
               JScrollPane.HORIZONTAL_SCROLLBAR_AS_
                    NEEDED);
          displayScroll.setVerticalScrollBarPolicy(
               JScrollPane.  
               VERTICAL_SCROLLBAR_AS_NEEDED);

          panel1.add(gameDisplay);
          panel1.add(displayScroll);

          setContentPane(panel1);

          this.setVisible(true);
     }
}

Everything workable fine when I run it, but when the text gets out of the bounds of the JTextArea, the scrollbars never appear! And yes, I know I'm using absolute positioning (no layout) and that's bad but I need it for other reasons in my game. Thanks in advance! (Also I can't connect to this site for some reason on my computer but I've used you guys before and you're awesome, so I have to type this on my phone. Sorry if the layout of the question gets screwed up, it the phone :P)

Was it helpful?

Solution

When you call...

panel1.add(gameDisplay);
panel1.add(displayScroll);

You are effectively removing the gameDisplay from displayScroll, as a component can only have one parent.

The fact that it works as it does comes down to the fact that you are playing around with the position and size of the gameDisplay panel instead of the displayScroll, which you should be...

Use

panel1.add(displayScroll);

Instead, but make sure you size and position ONLY the displayScroll, as displayScroll will take care of the gameDisplay

OTHER TIPS

I had a similar problem and the solution was setPreferredSize on the component inside the scrollbar. I was trying to absolute position things inside the scrollbars. Luckily I knew exactly what the size should be. Maybe this is a special case.

final JFrame frame = new JFrame("HelloWorldSwing");
final JPanel panel = new JPanel();
panel.setPreferredSize(PUT SIZE HERE);
panel.setLayout(null);
final JScrollPane scrollPane = new JScrollPane(panel);
frame.getContentPane().add(scrollPane);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top