File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes A question about JScrollPane. Thanks in advance! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "A question about JScrollPane. Thanks in advance!" Watch "A question about JScrollPane. Thanks in advance!" New topic

A question about JScrollPane. Thanks in advance!

Joe J. Wang
Ranch Hand

Joined: Feb 26, 2003
Posts: 67
Hi all,
I am new to the GUI/swing, so maybe you think the question is too simple, but please give some words if you know the answer. Thanks!!
What I am going to do is draw a big size custom Graphics2D in a JPanel, and then put the JPanel into a JFame. But the size of the Graphics2D/JPanel is too big, So must be scrollable. I tried to put the JPanel into a JScrollPane, then add the JScrollPane to the JFrame, the Graghics2D is there, but it just can't be scrollable. Shall I use other class?
I also tried to use awt.Canvas, but there is still problems. ON windows, no matter how small I resize the JFrame's size. The scroll bar just can't be showed, but it can be scrolled vetically by rolling the mouse(I am using JDK1.4.1, and I guess the Canvas blocks it, but I can't find how to make the Canvas not Opaque)
Then I tried on linux, This time, the scrollbar can be showed, but the Canvas can still block the scrollbar. The scrollbar can only be showed when the view moves to the right-bottom conner. Otherwise, the Canvas will block the scrollbar.
I read a book said that all the JComponent in the JScrollPane can be scrollable, but how can I do that?
Any information or suggestions will be highly appreciated!!

ashish kulkarni
Ranch Hand

Joined: Aug 15, 2002
Posts: 131
set the setPreferredSize of the JPanel and add it to the scrollpane and then add the scroll pane to the JFrame,
u will get the scrollbar here is a code example which u may look at

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestRepaint extends JFrame
public TestRepaint()
super("layered pane");
JScrollPane scrollPane = new JScrollPane(new MyPanel());
Container contentPane = this.getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
setSize(new Dimension(800,600));

class MyPanel extends JPanel
public MyPanel()
setPreferredSize(new Dimension(2000,600) );

public void paintComponent(Graphics g)
System.out.println ("Painting Back " );
int x = 0;
for(int i = 0; i < 60; i++)
x +=60;
g.drawLine(x, 0, x, 600);



public static void main(String args[])
new TestRepaint();



Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
I'm not sure on it, but maybe you will need to put the scrollbar policy to always.

By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Joe J. Wang
Ranch Hand

Joined: Feb 26, 2003
Posts: 67
Hi guys,
It is great!! ashish kulkarni just got me, yeah, I need to read about the whole chapter for JComponent. In my program, I used JPanel.setSize() instead of setPreferredSize(). Actually, the setSize is from java.awt.Component. Damn AWT!
Thank you VERY MUCH for replying my post!!
I agree. Here's the link:
subject: A question about JScrollPane. Thanks in advance!
It's not a secret anymore!