The BigBoss
The BigBoss

Reputation: 111

Adding a ScrollPane to JTextArea

I got a problem with this portion of code trying to get a ScrollPane on a JTextArea

showArea = new JTextArea();
showArea.setBounds(5, 145, 625, 310);
showArea.setBorder(BorderFactory.createLineBorder(Color.black));
showArea.setText(cat.getValidOffers());
scrollPane = new JScrollPane(showArea);  
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(scrollPane);
panel.add(showArea);

I really don't know what is wrong with this code, the scrollpane doesn't show at all.

Upvotes: 0

Views: 86

Answers (2)

Sorter
Sorter

Reputation: 10220

Don't set the bounds for the textarea. Also since you have already added the textarea inside scrollpane, you don't need to add it to the panel.

Upvotes: 1

Paul Samsotha
Paul Samsotha

Reputation: 208994

"I really don't know what is wrong with this, the scrollpane doesn't show at all."

Remove this panel.add(showArea);. You only need to add the scroll pane. A component can only have one parent container. When you add the text area to the panel, it is removed from the scroll pane

Also set the size of the text area like this

new JTextArea(20, 30);  .. rows, columns

And remove this

showArea.setBounds(5, 145, 625, 310);

Upvotes: 1

Related Questions