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 / June 2005

Tip: Looking for answers? Try searching our database.

JScrollPane responsive to cursor keys in 1.4, but not in 1.5

Thread view: 
Thomas Fritsch - 15 Jun 2005 13:18 GMT
Hi newsgroups!

I have a scrolling-savvy component for viewing a large image
inside a JScrollPane. The JScrollPane reacts to the cursor keys
(Up, Down, PageUp, PageDown, Home, End, ...), so that I am able
to scroll through the image *without* using the mouse.
This works fine with Java 1.4.2.

But with Java 1.5.0 the JScrollPane doesn't react to these keys.
(I even retested with Java 1.3.1: The JScrollPane doesn't react
there, too)

What is wrong with my code? Did I rely on an undocumented
1.4-feature? Has Sun silently removed this fine feature in 1.5 ?
How can I get this feature in 1.5 ?

Below is a short compilable example for reproducing the problem:

// Begin SSCCE
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class ImageComponent extends JComponent implements Scrollable
{
    public static void main(String args[])
    {
        JFrame frame = new JFrame("Scroll Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane scrollPane = new JScrollPane(new ImageComponent());
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    private BufferedImage image =  // a meaningless large image
            new BufferedImage(850, 1100, BufferedImage.TYPE_BYTE_GRAY);

    public Dimension getPreferredSize()
    {
        return new Dimension(image.getWidth(), image.getHeight());
    }

    protected void paintComponent(Graphics g)
    {
        g.drawImage(image, 0, 0, this);
    }

    // Implementation of Scrollable interface:

    public Dimension getPreferredScrollableViewportSize()
    {
        return new Dimension(Math.min(image.getWidth(), 500),
                            Math.min(image.getHeight(), 500));
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect,
            int orientation, int direction)
    {
        return getScrollableBlockIncrement(visibleRect, orientation,
                direction) / 10;
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect,
            int orientation, int direction)
    {
        switch (orientation)
        {
            case SwingConstants.HORIZONTAL: return visibleRect.width;
            case SwingConstants.VERTICAL:   return visibleRect.height;
            default: throw new IllegalArgumentException();
        }
    }
}
// End SSCCE

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Andrew Thompson - 15 Jun 2005 14:56 GMT
> // Begin SSCCE

?

> import java.awt.*;
> import java.awt.image.*;
[quoted text clipped - 11 lines]
>          frame.setVisible(true);
>      }

   public boolean getScrollableTracksViewportHeight() {
       return true;
   }

   public boolean getScrollableTracksViewportWidth() {
       return false;
   }

>      private BufferedImage image =  // a meaningless large image
>              new BufferedImage(850, 1100, BufferedImage.TYPE_BYTE_GRAY);
[quoted text clipped - 36 lines]
> }
> // End SSCCE

Probably not what you were expecting, but at least this
addition to the code allows it to compile successfully.

This raises the questions.  Is what you supplied the *actual*
code that displays the problem, and if so, how do you get it
to compile?  Alternately - what is the actual code?

BTW - it is difficult to tell if anything is happening unless
you paint a gradient on the image or add some println's..

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Thomas Fritsch - 15 Jun 2005 15:24 GMT
>>// Begin SSCCE
[...]
>>// End SSCCE
>
[quoted text clipped - 4 lines]
> code that displays the problem, and if so, how do you get it
> to compile?  Alternately - what is the actual code?
OK, Andrew, you are right.
My 2 methods getScrollableTracksViewportWidth and
getScrollableTracksViewportHeight slipped by
during copy/paste in my first post. :-(

> BTW - it is difficult to tell if anything is happening unless
> you paint a gradient on the image or add some println's..
Well, at least I could see the scrollbar jumping (or not jumping,
in the 1.5 case).
Anyway: I added a constructor drawing some text onto the image.

So here is the new one:

// Begin SSCCE
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class ImageComponent extends JComponent implements Scrollable
{
    public static void main(String args[])
    {
        JFrame frame = new JFrame("Scroll Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane scrollPane = new JScrollPane(new ImageComponent());
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    private BufferedImage image =  // a meaningless large image
            new BufferedImage(850, 1100, BufferedImage.TYPE_BYTE_GRAY);

    public ImageComponent()
    {
        Graphics2D g = image.createGraphics();
        for (int i = 1; i < 200; i++)
            g.drawString("Line " + i + ": bla bla bla", 0, i * 12);
    }

    public Dimension getPreferredSize()
    {
        return new Dimension(image.getWidth(), image.getHeight());
    }

    protected void paintComponent(Graphics g)
    {
        g.drawImage(image, 0, 0, this);
    }

    // Implementation of Scrollable interface:

    public Dimension getPreferredScrollableViewportSize()
    {
        return new Dimension(Math.min(image.getWidth(), 500),
                            Math.min(image.getHeight(), 500));
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect,
            int orientation, int direction)
    {
        return getScrollableBlockIncrement(visibleRect, orientation,
                direction) / 10;
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect,
            int orientation, int direction)
    {
        switch (orientation)
        {
            case SwingConstants.HORIZONTAL: return visibleRect.width;
            case SwingConstants.VERTICAL:   return visibleRect.height;
            default: throw new IllegalArgumentException();
        }
    }

    public boolean getScrollableTracksViewportHeight()
    {
        return false;
    }

    public boolean getScrollableTracksViewportWidth()
    {
        return false;
    }
}
// End SSCCE

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Andrew Thompson - 15 Jun 2005 17:23 GMT
...
>> BTW - it is difficult to tell if anything is happening unless
>> you paint a gradient on the image or add some println's..
> Well, at least I could see the scrollbar jumping (or not jumping,
> in the 1.5 case).
> Anyway: I added a constructor drawing some text onto the image.

I added a printlns to each method..

> So here is the new one:

I can confirm that it scrolls using Java 1.4.0, but not 1.5.0_03.

The printlns indicated that getScrollableUnitIncrement &
getScrollableBlockIncrement were being called by Java 1.4,
but not 1.5.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Christian Kaufhold - 15 Jun 2005 17:00 GMT
> I have a scrolling-savvy component for viewing a large image
> inside a JScrollPane. The JScrollPane reacts to the cursor keys
[quoted text clipped - 9 lines]
> 1.4-feature? Has Sun silently removed this fine feature in 1.5 ?
> How can I get this feature in 1.5 ?

Your component is not focusable. In 1.4, the scrollbars are, and they
get the focus. For me, also only scrolling in *one* direction works
(depending on which scroll bar has the focus).
If your component has the focus, scrolling will work (unless it
swallows the cursor keys.

Christian
Thomas Fritsch - 15 Jun 2005 18:18 GMT
Christian Kaufhold schrieb:

>>I have a scrolling-savvy component for viewing a large image
>>inside a JScrollPane. The JScrollPane reacts to the cursor keys
[quoted text clipped - 17 lines]
>
> Christian

Thank you very much for these hints, Christian. I added
    scrollPane.setFocusable(true);
And now it works in 1.5 and 1.4 (even better than before,
because scrolling in *both* directions works correctly now)

Thomas
Thomas Fritsch - 15 Jun 2005 18:31 GMT
Thomas Fritsch schrieb:
> Christian Kaufhold schrieb:
>
[quoted text clipped - 8 lines]
> Thank you very much for these hints, Christian. I added
>     scrollPane.setFocusable(true);
      scrollPane.getHorizontalScrollBar().setFocusable(false);
      scrollPane.getVerticalScrollBar().setFocusable(false);
> And now it works in 1.5 and 1.4 (even better than before,
> because scrolling in *both* directions works correctly now)
>
> Thomas


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.