2

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); 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2845399
  • 101
  • 2
  • 10
  • you have to fill the area for the scrollpane to appear – RamonBoza Oct 04 '13 at 09:07
  • I filled the scroll area for the same but still nothing seems to happen.. – user2845399 Oct 04 '13 at 09:09
  • 2
    1) `textArea.setPreferredSize(new Dimension(100,600));` The size of a `JTextArea` can be better specified in the number of rows and columns used in the constructor. 2) `scrollpanel.setBounds(20, 600, 920, 130);` Doing that (suggesting a size) will probably mean that this nonsense can go. .. – Andrew Thompson Oct 04 '13 at 09:31
  • .. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Oct 04 '13 at 09:31
  • scrollbar is displaying when i run my code but problem is, suppose the length of the string is more than the width of the textarea then it will no Scrollable.For example suppose i am displaying this statement "Hello I am XYZ . How are you ?". Its display only "Hello I am XYZ . How a" only remaining part is not showing. – user2845399 Oct 04 '13 at 09:39
  • On the otherhand,vertical scroll is working absolutely fine – user2845399 Oct 04 '13 at 09:39
  • One [related example](http://stackoverflow.com/a/17626931/1057230)and [another example](http://stackoverflow.com/a/13098108/1057230), hope it helps :-) – nIcE cOw Oct 04 '13 at 11:30

2 Answers2

3

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • As you said, I tried same but when i remove the setbound method and override the scroll pane's getPreferredsize(), textArea is not show into my mainframe. – user2845399 Oct 04 '13 at 16:45
  • Be sure to `pack()` the frame _after_ adding components and _before_ calling `setVisible`. – trashgod Oct 04 '13 at 18:26
  • yeah i am sure pack() the frame after adding components before adding components and before setVisible() – user2845399 Oct 05 '13 at 05:42
  • If you are still having problems, it may help to edit your question with an [sscce](http://sscce.org/) that exhibits the problem you describe. – trashgod Oct 05 '13 at 08:36
2

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

}
The_Lost_Avatar
  • 992
  • 5
  • 15
  • 35