While working on some sandbox code to post here I was unable to get the
same glitch to manifest itself. What I discovered after closer
examination was this:
public void paintComponent(Graphics g)
{
//textPane.setSize(getWidth()-30,getHeight()-150);
scrollPane.setSize(getWidth()-30,getHeight()-150);
textField.setSize(getWidth()-30,25);
textField.setLocation(10,textPane.getHeight()+20);
send.setLocation(getWidth()/2,textField.getY()+35);
cancel.setLocation(getWidth()/2-110,textField.getY()+35);
super.paintComponent(g);
}
I had overridden the JInternalFrame's paintComponent method (above),
and the commented line (then uncommented) was evidently resetting the
value of the attached JScrollPane's JScrollBar.
So, lessons learned:
1. Calling a repaint on the JScrollPane is sufficient for JComponent
resizing (one need not call the attached components as well).
2. Calling a repaint on the attached members of a JScrollPane seems
to reset the underlying JScrollBar's value.
Working code looks like:
public void paintComponent(Graphics g)
{
scrollPane.setSize(getWidth()-30,getHeight()-150);
textField.setSize(getWidth()-30,25);
textField.setLocation(10,getHeight()-130);
send.setLocation(getWidth()/2,textField.getY()+35);
cancel.setLocation(getWidth()/2-110,textField.getY()+35);
super.paintComponent(g);
}
Thanks for the interest/help Oliver.
Andrew Thompson - 28 Dec 2006 13:53 GMT
> While working on some sandbox code to post here I was unable to get the
> same glitch to manifest itself.
...
> Working code looks like:
I doubt that the real problem is fixed*.
> public void paintComponent(Graphics g)
> {
[quoted text clipped - 5 lines]
> super.paintComponent(g);
> }
This code is extremely fragile, and will might break
under the next Java version, on a different screen,
with different screensize, or under a different PLAF..
You should proceed with posting SSCCE's**, or at least
* figure why the behaviour suddenly disappears.
** <http://www.physci.org/codes/sscce>
Andrew T.
Oliver Wong - 28 Dec 2006 15:17 GMT
> public void paintComponent(Graphics g)
> {
[quoted text clipped - 5 lines]
> super.paintComponent(g);
> }
It looks like you're trying to reposition and resize your widgets every
time the JFrame (or whatever the containing component is) needs to be
redrawn. This is generally frowned upon, and the recommended practice is to
use a LayoutManager to position and size the components for you. See this
tutorial for more details:
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
- Oliver