I currently have a Java app that is using an instance of JTextArea
to present data to the user. They press a button, and data stored from a database is populated.
There is no vertical scrollbar, and the tables in the database contain more rows than can fit on the screen.
How can I add a vertical scrollbar to this text area?
Many portions of the code depend on writing to the JTextArea
and it would be a huge time cost to refactor the code to adapt to printing to another type of container.
Is there any way to wrap the JTextArea
into a ScrollPane
?
Current code (which displays textArea without scroll bars):
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1057, 484);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea mainTextArea = new JTextArea();
mainTextArea.setLineWrap(true);
mainTextArea.setWrapStyleWord(true);
mainTextArea.setBounds(21, 93, 995, 336);
frame.getContentPane().add(mainTextArea);
// (continued...)
My attempt at wrapping the code in a scroll pane (neither the text area nor the scroll bars appear):
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1057, 484);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea mainTextArea = new JTextArea();
mainTextArea.setLineWrap(true);
mainTextArea.setWrapStyleWord(true);
mainTextArea.setBounds(21, 93, 995, 336);
JScrollPane scroll = new JScrollPane (MainTextArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scroll);
frame.getContentPane().setVisible(true);
// (continued...)