Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2006

Tip: Looking for answers? Try searching our database.

JScrollBar Repaint Question

Thread view: 
mearvk - 22 Dec 2006 06:04 GMT
I have a JInternalFrame which contains various JComponents. One of
which is a JScrollPane which contains a JScrollBar. When I do a repaint
call (I have an ActiveRepainter class) on the JDesktopPane the
JScrollBar resets itself to the topmost position. I want the JScrollBar
to be in the same position it was in prior to the paint call.

I have some ideas but none of them are elegant and before I spend more
time trying to solve this I would like to get some input. Perhaps
overriding the paint method of the JScrollBar is the best solution?

Thanks for the help,

Mearvk
mearvk - 22 Dec 2006 15:40 GMT
For some reason the JScrollBar keeps setting its value to 3...

So if I subclass JScrollBar (see code) and trap for this anomaly, I
have kludged success.
The call to JScrollBar's paint (in case it is pertinent) is coming via
a repaint() call to a JDesktopPane. The JDesktopPane calls the
JInternalFrame which call its subcomponents (of which the JScrollBar is
a member). This is my understanding of the situation, at least..

public void paint(Graphics g)

{

int curVal=super.getValue();

if(curVal==3)
{
  super.setValue(lastGoodValue);
}
else
{
  lastGoodValue=curVal;
}

System.out.println("Painting with "+super.getValue());
super.paint(g);

}

So, of course, I am left to wonder why this is happening.

Any thoughts?

Mearvk
Oliver Wong - 27 Dec 2006 21:23 GMT
> For some reason the JScrollBar keeps setting its value to 3...
>
[quoted text clipped - 28 lines]
>
> Any thoughts?

   Post an SSCCE demonstrating the problem:
http://mindprod.com/jgloss/sscce.html

   - Oliver
mearvk - 27 Dec 2006 22:18 GMT
So, basically, I will attempt (in lieu of slapping my code up here) to
describe the cause of the problem. Whenever I move the JInternalFrame
the JScrollBar resets its value to a number near 0. Whenever I repaint
the JDesktopPane the same thing occurs. The desired behavior is that
the ScrollBar stay in the same location despite a JInternalFrame move
or a JDesktopPane repaint.

I subclassed JScrollBar and perform a similar if statement as above. It
works but I would prefer the correct solution.

Thanks,

Mearvk

PS If you still need to see code after this explanation I will be happy
to post it. I don't think there is anything revealing in it however.
Oliver Wong - 27 Dec 2006 22:28 GMT
> So, basically, I will attempt (in lieu of slapping my code up here) to
> describe the cause of the problem. Whenever I move the JInternalFrame
[quoted text clipped - 12 lines]
> PS If you still need to see code after this explanation I will be happy
> to post it. I don't think there is anything revealing in it however.

   Yes, the reason I asked for the code would be so:

   (1) I could run the code and see if I can actually reproduce the
problem. If I can't, then maybe all you need to do is switch to the same JVM
that I'm using.
   (2) If the problem does occur, to pause it in my debugger and
investigate what's going on.

   Note that you should NOT post your actual code, but instead, a short,
self contained, compilable example. I will not run the source code without
reading it first to check against trojan code, and if it is too long, then I
won't bother reading it.

   - Oliver
mearvk - 28 Dec 2006 10:33 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.