I am trying to add a vertical and horizontal scroll bar to a text area though its not working.
JTextArea notices = new JTextArea(""); JScrollPane pane = new JScrollPane(notices, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); notices.setPreferredSize(new Dimension(300,300)); notices.setBackground(Color.YELLOW); int index = 0; try{ BufferedReader reader = new BufferedReader(new FileReader(file)); String line = ""; while((line=reader.readLine())!=null){ notices.append("Notice "+ ++index +":\r\n"); notices.append(line + "\r\n"); } reader.close(); }catch(Exception except){ JOptionPane.showMessageDialog(mainPanel, "Sorry could not be done"); } notices.setEditable(false); notices.setVisible(true); center.add(pane);
The above code gives a scroll pane on the vertical side though it does not allow me to scroll down when the text exceeds the boundaries of the JTextArea. If I change JScrollPane settings to VERTICAL_SCROLLBAR_AS_NEEDED and HORIZONTAL_SCROLLBAR_AS_NEEDED I don't get anything at all.