Java swing: Add scroll bar to textarea

107 Views Asked by At

I have a text area that updates a button press (this updating works fine). When it updates, a long list appears in the text area that requires a scroll bar. However, when I add the last two lines below, not only does the scroll bar not appear, text doesn't appear as well. How do I fix this? (I'm new to java swing so the simpler the answer the better)

textArea1 = new JTextArea("");
textArea1.setBounds(450, 40, 400, 700);
frame.getContentPane().add(textArea1);
JScrollPane scroll = new JScrollPane(textArea1);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
1

There are 1 best solutions below

0
Gilbert Le Blanc On

You add the JScrollPane to the JFrame.

textArea1 = new JTextArea("");
textArea1.setBounds(450, 40, 400, 700);
JScrollPane scroll = new JScrollPane(textArea1);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scroll);

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

You should be using Swing layout managers, rather than setBounds.