Can't make JScrollPane work

Hello!

I'm just trying to make this have those things that you can scroll with.

Here is my code, it's retty close to a exemple code from a O'reilly book but I can't make it work.

(In case it's important DrawingArea extends JPanel)

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import java.awt.Color;

publicclass SlidingWindows{

publicstaticvoid main(String[] args){

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

DrawingArea pane =new DrawingArea();

JScrollPane scroll =new JScrollPane(pane);

frame.getContentPane().add( scroll );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.setSize( 500, 200 );

frame.setVisible(true );

}

}

[1303 byte] By [kotokoa] at [2007-11-27 10:49:42]
# 1

Are you sure the DrawingArea has a size different than 0,0 ?

Dalzhima at 2007-7-29 11:20:54 > top of Java-index,Java Essentials,New To Java...
# 2

Yes since everything is displayed exactly the way it should except that there is no way to scroll.

kotokoa at 2007-7-29 11:20:54 > top of Java-index,Java Essentials,New To Java...
# 3

Your code is working but what you are putting inside the scroll pane is resizing so it never needs to scroll (unless you make it really small) try the code below which uses a JTextArea to demonstrate:

public class SlidingWindows {

public static void main(String[] args) {

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

JPanel panel = new JPanel();

JTextArea textArea = new JTextArea(100,200);

panel.add(textArea);

JScrollPane scroll = new JScrollPane(panel);

frame.add(scroll);

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.setSize( 500, 200 );

frame.setVisible( true );

}

}

ita6cgra at 2007-7-29 11:20:54 > top of Java-index,Java Essentials,New To Java...
# 4

When I tried it with

frame.setSize( 50, 20 );

It didn't work (I'm resizing it this small so I can see if it works without having to show you the real program wich is too big - what will be displayed on the screen is huge and will allways need a scroll).

kotokoa at 2007-7-29 11:20:54 > top of Java-index,Java Essentials,New To Java...
# 5

My fortee is not really swing but I think the scroll pane only ever applies to whats within it so no matter what size you set the frame what really matters is the size of the content (In my example the text area) Theres a tutorial here:http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html

That may be of more help

ita6cgra at 2007-7-29 11:20:54 > top of Java-index,Java Essentials,New To Java...