Problems with JScrollPane
Hello guys!I'm playing around with a JScrollPane.
Please look at this image at:
http://fredand44.webs.com/bilder/test.png
Look how bad and strange it scrolls my canvas inside the JScrollPane.
I thougt it would scroll the view downwards, but instead it scrolls the canvas upwards and over the border!!
If any one got any solution for this please let me know.
The code is below, compile, run and resize it a bit smaller, then try the scroll:
package test;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
public class TestScroll extends JPanel
{
public TestScroll(Canvas canvas)
{
TitledBorder titledBorder = BorderFactory
.createTitledBorder("Test scroll");
titledBorder.setTitleColor(Color.GRAY);
titledBorder.setTitleJustification(TitledBorder.LEFT);
setBorder(titledBorder);
JScrollPane scroll = new JScrollPane(canvas,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
setLayout(new BorderLayout());
add("Center", scroll);
}
public static void main(String[] args)
{
JFrame jFrame = new JFrame("Test");
MyCanvas canvas = new MyCanvas();
canvas.setBackground(Color.red);
TestScroll testScroll = new TestScroll(canvas);
jFrame.getContentPane().add(testScroll);
jFrame.setSize(400, 400);
jFrame.setVisible(true);
canvas.repaint();
}
}
class MyCanvas extends Canvas
{
public void update(Graphics g)
{
g.drawString("Hello Amigos", 10, 10);
}
}
Best regards
Fredrik