... why do you think the preferred size should change based on the content in the client component? Of course not :s The viewable area (and thus the preferred size) stays the same; the thing that changes in size is the -scrollable- area.
I made the following changes:
1. added the line: JOptionPane.showMessageDialog(null,sp); at the end of the handler.
2. commented out the line: sp.setPreferredSize(new Dimension(100,70));
I found that the JScrollPane keeps getting bigger with adding more text. However, when I removed the comment and set the preferred size of the JScrollPane, the size remained fixed.
This shows that the preferred size of the JScrollPane respond to the client change provided that it doesn't get set directly. If this is the case, the question is why?
I need to keep the size of the JScrollPane inside the screen, so if the JTextArea is too big to fit, I will set the preferred size of the JScrollPane directly; if the JTextArea is small enough to fit, I will not set the preferred size and leave it to the JScrollPane to detemive its own size based on the size of the client. In my case, JScrollPane behaves as expected; but once I set its preferred size (when the client gets too big), it stops responding to the size of the client when the client gets smaller.
I checked the API but couldn't get an answer.