Reputation: 1372
I have a JEditorPane that I need to attach a scrollbar to. I am attempting to nest the JEditorPane inside a JScrollPane. Here I provide an SSCCE.
import java.awt.Dimension;
import javax.swing.*;
public class Test{
public static void main(String[] args){
JFrame frame = new JFrame();
//Generate filler text to illustrate the lack of a scrollbar
String fillerText = "";
for(int i = 0; i < 500; i++){
fillerText += "The quick brown fox jumped over the lazy dog. ";
}
//Initialize JEditorPane
JEditorPane viewer = new JEditorPane();
viewer.setPreferredSize(new Dimension(500, 600));
viewer.setText(fillerText);
//Initialize JScrollPane
JScrollPane scroll = new JScrollPane(viewer);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//Add viewer to the frame
frame.add(viewer);
//Make frame visible
frame.pack();
frame.setVisible(true);
}
}
Why is no scrollbar made visible, and how can I make one visible?
Upvotes: 0
Views: 45
Reputation: 10994
Your problem in next: you add JEditorPane
to your JFrame
instead of JScrollPane
. When you need to use JScrollPane
you add component to that view and add JScrollPane
to container.
Replace frame.add(viewer);
with frame.add(scroll);
and it will be work.
Upvotes: 2