How can I make scrollbars appear around a JPanel when using the paintComponent() method?
How can I make scrollbars appear around a JPanel when using the paintComponent() method?
January 28, 2012 - 04:37
Hello all
How can I make scrollbars appear around a JPanel I use to draw "things" in? These "things" are graphical objects that are surely bigger than the current viewport, but nevertheless no scrollbars appear, even though I put a JScrollPane around.
Many thanx in advance
Keksi
Here is a simple demo to show the effect:
import java.awt.*;
import javax.swing.*;
public class GraphicsDemo extends JPanel
{
static public void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(new GraphicsDemo()), BorderLayout.CENTER);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
protected void paintComponent(Graphics g)
{
g.setColor(Color.GRAY);
g.fillRect(10, 10, 300, 50);
g.setColor(Color.BLACK);
g.drawRect(10, 10, 300, 50);
g.setColor(Color.GRAY);
g.fillRect(10, 60, 50, 300);
g.setColor(Color.BLACK);
g.drawRect(10, 60, 50, 300);
}
}




I found the answer by myself: this implementation of the paintComponent() method works fine: