martin
martin

Reputation: 1007

How to place the scrollbar of a JScrollPane in a custom position?

So I have a JTextArea and right next to it I have placed a JLabel with the same height. I want to make the JTextArea scrollable but when I do so, the scrollbar goes between the JTextArea and the JLabel. This is the default but I want to place the scrollbar on the right of the JLabel so that it scrolls the JTextArea but does nothing to the JLabel. The JLabel contains small image, nothing else and I want that image to be between the JTextArea and it's scrollbar. What is a good way to achieve that?

Upvotes: 1

Views: 232

Answers (2)

Michael Dunn
Michael Dunn

Reputation: 816

1) add your textArea to a scrollPane set with verticalScrollbar.NEVER and horizontalScrollbar.NEVER (textArea would need to have wordWrap set)

2) add the scrollPane to a JPanel(borderLayout) at BorderLayout.WEST

3) add your label to the panel at BorderLayout.CENTER

4) add a JScrollBar to the panel at BorderLayout.EAST

5) get the verticalScrollbar of the scrollPane and set its model to that of the scrollBar at BorderLayout.EAST

if the frame is resizable, and you want the textArea/scrollpane to take the additional/less space, (2) and (3) (or (3) and (4)) would need to be combined into a separate panel so that the textArea/scrollPane is in the CENTER area of a panel

Upvotes: 2

DankMemes
DankMemes

Reputation: 2137

Make a JPanel, and put the textarea and the image in it, and lay it out horizontally. then set the panel as a JScrollPane's viewport view.

Upvotes: 0

Related Questions