Posted on Jun 6, 2007

Full size JComponent in JScrollPane

I came across a common problem the other day that I am sure many of you have. As it had been ages since I last solved it I turned to Google hoping it would come to my rescue. Well either I wasn’t guessing the right search terms or the answer is not out there. So I thought I would help the situation by writing a blog with the answer.

So the problem is you have some component in a scroll pane and when its preferred height is less than the scollpane area then you want it to stretch to fill the scollpane. But when it grows bigger than the scollpane area you would like it show the scrollbars. This is very useful when you want to be able to click in the spare space to clear selection or in the case of drag and drop where you want to be able to drop into the empty area. There was a bug fixed in JDK 6 that made this possible for JTable with setFillsViewportHeight(true). So here is how you do it for your own components: You need to make the component that is in the JScrollPane implement javax.swing.Scrollable and below is a skeleton implementation for you.

    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return (orientation == SwingConstants.HORIZONTAL)? visibleRect.width : visibleRect.height;
    }

    public boolean getScrollableTracksViewportHeight() {
        boolean tracks = false;
        if (getParent() instanceof JViewport) {
            tracks = getPreferredSize().getHeight() <
                    ((JViewport) getParent()).getExtentSize().getHeight();
        }
        return tracks;
    }

    public boolean getScrollableTracksViewportWidth() {
        return true;
    }

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

The key part of this is the getScrollableTracksViewportHeight() method that adjusts its returned value based on the relationship between preferred height and visible height. You could use something similar for getScrollableTracksViewportWidth() if you want a similar effect in that direction. Now I just have to remember to look at my blog next time I need to do this ;-)

Posted on May 29, 2007

San Francisco Zoo Photos

Fiona and I went to San Francisco Zoo on the weekend. We remembered to take sun cream and hats after getting burnt last week. What we forgot is the weather is totally different in SF to Santa Clara. We got there and its was cold and gray. What we didn’t pack was any warm cloths so we joined the main people walking around the zoo in SF Zoo hoodies. Many of the animals were wisely hiding indoors but got a few nice photos.

Posted on May 29, 2007

Java One Photos

I am finally getting a chance to go though the 20Gb of photos I have taken in the last couple weeks. So lots more to come. My first Java One was an amazing experience and it was very cool to be part of the Technical General Session with the IRIS demo. Checkout the behind the scenes photos of all the hardware driving the main screens.

Posted on May 15, 2007

IRIS Video on YouTube

The IRIS video is now available on YouTube.

Posted on May 11, 2007

Why Spaghetti Is Not Tasty: Architecting Full-Scale Swing Apps

I think the talk went well yesterday, the room was almost full. It was very enlightening to talk to people afterwards as many said they had either built frameworks on the same technologies of Swing, OSGi and IOC or are currently looking into doing so. The concept of Event/Message Bus was also something that many people have adopted or are investigating adopting. I am glad I got to do the talk as it sounds like many people have had the same issues designing and managing large scale swing application. I hope I got a chance to help some people who are lucky enough to be at an early stage in developing a application and can adopt some of these solutions.

Click below for a PDF of the slides for the talk:

slide one

The SPAR Framework

During my talk I introduced the SPAR framework which is the rich client framework I built for Imagery. I have created an open source project for it at spar.dev.java.net. It is under a BSD license so you are free to use it how you like. If people are interested in signing up to help improve the SPAR framework and move it forward that would be great!

I will work on some form of documentation for the framework in the near future so I will apologize now for being light on documentation. There is a sample application as a very simple worked example which doesn’t do much at the moment other than bring up a window with docking panels, menus, toolbars etc. There are two plugins that make up the SPAR framework and two docking framework plugins at the moment:

  • spar – This is the core of the framework and has no concept of UI so can be used for headless applications.
  • spar.ui – This is the UI framework for menus, toolbars, docking panels etc.
  • spar.ui.jide – This contains the JIDE libraries, a implementation of spar.ui’s DockingManager and some custom components I have written that depend on JIDE.
  • spar.ui.infonode – This will be a InfoNode implementation of spat.ui’s DockingManager but I have not had a chance to finish this yet.

I hope the SPAR framework turns out to be useful and I am interested in hearing peoples feedback on it. I plan to blog more about it in the future and explain more about how it works.