JScrollPane not working

After a lot of digging of internet (and googling ^_^) I came up with the following code for using a JScrollPane (the DrawingArea extends JPanel).

I can't go any further since *all* the stuff I found was for images and scrolling. I have no idea how to handle something that is just a JPanel.

import javax.swing.JFrame;

import javax.swing.JScrollPane;

publicclass SlidingWindows{

publicstaticvoid main(String[] args){

JFrame frame =new JFrame("Sliding windows" );

DrawingArea pane =new DrawingArea();

JScrollPane scroll =new JScrollPane();

scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

scroll.setViewportView(pane);

frame.getContentPane().add( scroll );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.setSize( 100, 100 );

frame.setVisible(true );

}

}

[1482 byte] By [kotokoa] at [2007-11-27 10:51:30]
# 1

Your DrawingArea class needs to implement the Scrollable interface since JPanel doesn't.

dwga at 2007-7-29 11:31:31 > top of Java-index,Java Essentials,New To Java...
# 2

In the future, Swing-related questions should be posted in the Swing forum.

I think your problem is that the panel's preferredSize isn't bigger than that of the scrollPane viewing area. Try this:

import javax.swing.*;

import java.awt.*;

public class SlidingWindows

{

public static void main(String[] args)

{

JPanel pane = new JPanel();

pane.setPreferredSize(new Dimension(400,400));

JScrollPane scroll = new JScrollPane(pane);

scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

JFrame frame = new JFrame( "Sliding windows" );

frame.getContentPane().add( scroll );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.setSize( 200, 200 );

frame.setVisible( true );

}

}

You should now see the scrolling effect that you're looking for. If you stretch the frame larger, the scroll thumbs will disappear. If you shrink the frame back, they will reappear, etc.

Normally, the JPanel's preferredSize will be measured by the total preferredSize of it's contents. Since the JPanel was blank, I think it might default to something small like 10x10.

> Your DrawingArea class needs to implement the Scrollable interface since JPanel doesn't.

I've never had to do this.

KelVarnsona at 2007-7-29 11:31:31 > top of Java-index,Java Essentials,New To Java...
# 3

Ok so I did the pane.setPreferredSize(new Dimension(400,400));

thing I now it works - thanks a million for that!

However now it only scrolls exactly how big I set the preferred size (regardless of if it's blank or not) and it blurs when I scroll.

So maybe I should be more specific about the DrawingArea.

It extends JPanel and then just has loops that use paintString and fillRect.

I guess it would be better coding if I transformed the paintStrings in some kind of objects (maybe JLabel?) but about the rectangles I don't thin that there is much I can do.

Any sugestions? If someone could just help me with the blurring problem would be great.

kotokoa at 2007-7-29 11:31:31 > top of Java-index,Java Essentials,New To Java...