Reputation: 73
I trying to make a game where I add a Canvas to a JScrollPane but the canvas is larger than the visible area of the JScrollPane. So when I scroll the JScrollPane to see the rest I only see the blank area even though my Canvas is continuously redrawn. Anybody can help me??
Upvotes: 2
Views: 3711
Reputation: 141
It is common JScrollPane problem. To fix this with minimum fuss, call the following methods on your JScrollPane:
scrollpane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
Upvotes: 10
Reputation: 1
You need to use a lightweight JPanel and 2 heavyweight objects like:
JPanel foreground = new JPanel();
Panel frame = new Panel();
Cavas canvas = new Cavas();
foreground.add(frame);
frame.add(canvas);
Heavyweight objects might be AWT, or SWT (with OLE), or mixed, it does not matter.
Then get JScrollPane and add AdjustmentListener which calls method like:
private void updateLocation() {
final JScrollPane scroll = getScrollPane();
if (null == scroll) {
// do nothing
return;
}
final Point top = getLocation();
final Rectangle visible = getVisibleRect();
// set up new location
frame.setSize(visible.width, visible.height);
frame.setLocation(visible.x - top.x, visible.y - top.y);
canvas.setSize(getWidth(), getHeight());
canvas.setLocation(top.x - visible.x, top.y - visible.y);
}
Upvotes: 0
Reputation: 66
Swing components do not work with heavyweight AWT components like Canvas. Use a JComponent or a JPanel instead of a Canvas
Upvotes: 3