0

I've been trying for hours to get a simple scrolling panel. Let's say I'm trying to display a large image on a panel (too large to be seen all at once) and therefor want to be able to move the panel displaying this image around using setLocation() prompted by some method we'll call scroll(). I am aware of scroll panels that require the mouse to move the bars on the side, but I do not want this. I want it to move based off of a keyboard input/method call. How do I fix this? Here's a basic version of what I'm doing:

Frame f = new JFrame();
Panel p = new JPanel();

f.setSize(2000,2000);
p.setLocation(X, Y);
f.getContentPane().add( mainPanel);
f.repaint();

This will display the JPanel at the same location, no matter what X and Y are. If I set the layout to null it doesn't display at all. Please help me

  • 1
    Go to Google, type "scrolling jpanel". – sjr Oct 06 '13 at 01:55
  • For [example](http://stackoverflow.com/a/5797965/230513). – trashgod Oct 06 '13 at 01:55
  • `I want it to move based off of a keyboard input/method call.` - I hope you are using [Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for this. – camickr Oct 06 '13 at 02:38

1 Answers1

3

Read the JScrollPane and JViewport API. Calling setLocation should do nothing of use for you. Instead you have to change what the viewport is looking at.

The viewport's setViewPosition(...) could work for instance. And nulls layouts are almost always never the answer.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373