I'm trying to create a scroll pane for my jtextarea. Below is my code snippet. I wonder why it didn't work. Can someone provide me some insights? Thanks.
JTextArea textArea = new JTextArea(st);
textArea.setBounds(145, 51, 327, 53);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
//ScrollPane Code
JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
areaScrollPane.setPreferredSize(new Dimension(250, 250));
panel.add(textArea);
Did I miss something? There is no scrollpane appear in my textarea开发者_StackOverflow.
You're adding textArea
to panel instead of areaScrollPane
. Try
panel.add(areaScrollPane);
精彩评论