I am making a GUI in which i am adding the horizontal scroll bar in to TextArea because the length of the label/line that will be display in the TextArea is more than the width of the TextArea.
This is my code where I create the pane. But nothing seems to happen....
//create the text area for description and add into the main frame
public static JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setPreferredSize(new Dimension(100,600));
scrollpanel = new JScrollPane(textArea);
scrollpanel.setBounds(20, 600, 920, 130);
scrollpanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollpanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollpanel.setBorder(BorderFactory.createTitledBorder("Description"));
frmToolToMigrate.getContentPane().add(scrollpanel);
Instead of setBounds()
, override the scroll pane's getPreferredSize()
, as shown here. By default, the scroll bars will appear automatically as required. Also consider implementing the Scrollable
interface.