Hi,
I would like to make a window with a given size, and a scrollable textarea to a given place within.

When I tried the same with a button instead of textarea, it was no problem: make a JFrame, put a JPanel inside, put a JButton on the panel. Then call myPanel.setLayout = null to kill the layout manager, and myButton.setBounds(...) to place the button where I want.

The problem is, I tried to use a JScrollPane and a JTextArea in the same way, but setting myScrollPane.setLayout = null makes the scrollbar not appear. If I do use the layout manager, the scrollbar does appear, but the attempts to force the placement usually ended in strange results.

Do you have any idea how to fix it?

Dani AI

Generated

Short answer: don’t replace the JScrollPane’s own layout manager. A JScrollPane uses an internal layout to position its viewport and the two scrollbars; removing or replacing that layout prevents the scrollbars from being managed and they won’t appear or behave correctly. ’s JButton example worked because a simple component doesn’t contain its own sub-layout; a JScrollPane does.

Preferred approach: give the scroll pane a size via the parent’s layout (or set its preferred size) and let the scroll pane manage its internals. Example with GridBagLayout (keeps the scrollbars working and lets you control where the pane sits):

JTextArea ta = new JTextArea(8, 30);
JScrollPane sp = new JScrollPane(ta);
sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

JPanel p = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; c.gridy = 0;
c.weightx = 1.0; c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(8,8,8,8);

p.add(sp, c);
frame.setContentPane(p);
frame.pack();

If you truly need absolute positioning, set the parent container to no layout and set bounds on the JScrollPane instance itself — do not change the scroll pane’s internal layout. Also remember to call revalidate()/repaint() after resizing or swapping components.

Quick troubleshooting:

  • If no vertical scrollbar appears, confirm the text actually exceeds the viewport (or force policy to ALWAYS to test).
  • If you enabled line wrap on the text area, horizontal scrolling may be suppressed.
  • Use setPreferredSize on the scroll pane when using layout managers, and use setViewportView(...) if you replace the view manually.

Note: ’s CSS suggestion applies to HTML pages, not Swing components.

you can add a id to the text area,and add the follwing attribute for the corresponding id in the head tag like

<html>
<head>
#scrolltext
    {
        overflow: scroll; 
        overflow-y: scroll; 
        overflow:-moz-scrollbars-vertical;
    }


</head>
<body>
<textarea rows="4" cols="20" id="scrolltext"> </textarea>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.