user2845399
user2845399

Reputation: 111

Adding scrollpane on TextArea

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); 

Upvotes: 2

Views: 4889

Answers (2)

trashgod
trashgod

Reputation: 205785

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.

image

Upvotes: 3

The_Lost_Avatar
The_Lost_Avatar

Reputation: 990

Have you looked at

http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

You can also make textArea as a new class that extends TextArea java class and then implement scrollable.

public class MyTextArea extends JTextArea implements Scrollable{

//Whatever you want to do

}

Upvotes: 2

Related Questions