views:

61

answers:

2

1. JTextArea messages = new JTextArea(5, 30); 2. JScrollPane scrollTextBox = new JScrollPane(messages, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); 3. Container window; 4. ... 5. messages.setPreferredSize(new Dimension(500,100)); 6. messages.setLineWrap(true); 7. messages.add(scrollTextBox, BorderLayout.CENTER); 8. window.add(messages); 9.

10. I'm trying to write a text box within a GUI box that returns messages from a server, it needs a scroll bar because messages are continually being sent but at the moment only the first few lines are visible and the rest aren't because i can't get the scroll to work. 11. my attempts before have either resulted in this error: 12. java.lang.IllegalArgumentException: adding container's parent to itself 13. at java.awt.Container.checkAddToSelf(Unknown Source) 14. at java.awt.Container.addImpl(Unknown Source) 15. at java.awt.Container.add(Unknown Source) 16. at LODGUI.drawAndShow(LODGUI.java:91) 17. at LODClient.run(LODClient.java:94) 18. at LODClient.main(LODClient.java:157) 19.

20. or no change and no scroll bar.

A: 

The problem is explained in the error message. You are adding messages to scrollTextBox, then adding scrollTextBox to messages.

And please please try to format you question...

fish
A: 

Delete line 7.

Change line 8 to:

window.add(scrollTextBox); 

(The way to do it is to insert your component 'messages' into a JScrollPane which you do in line 2. Then, don't insert 'messages' into the container, but insert the scoll pane.)

And please, next time format your question.

Thomas
thanks, i sorted it out in the end.sorry bout the formatting, first time using this site and i'm just finding my way around.