wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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!!


SCJP, SCJD, SCWCD, SCEA I.
ashish kulkarni
Ranch Hand

Joined: Aug 15, 2002
Posts: 130
Hi,
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");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(new MyPanel());
Container contentPane = this.getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
setSize(new Dimension(800,600));
this.setVisible(true);
}


class MyPanel extends JPanel
{
public MyPanel()
{
setPreferredSize(new Dimension(2000,600) );
this.setBackground(Color.white);
this.setOpaque(true);

}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println ("Painting Back " );
g.setColor(Color.blue);
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();

}

}


A$HI$H
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.


Younes
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: http://aspose.com/file-tools
 
subject: A question about JScrollPane. Thanks in advance!
 
Similar Threads
Scrolling BufferedImage
Canvas in JScrollPane
Problem with Layouts
JScrollPane and Canvas problem
Changing component inside a JScrollPane