tags:

views:

501

answers:

6

I'm new to Java so this is probably pretty simple, but I'm not sure how to make it work.

Basically, I have a class which I use to draw a grid, which extends JPanel. I put this class in a window, inside a JScrollPane. The scroll works the way I want it to, except the drawing inside my class doesn't update itself once the window scrolls. It does update if the window is resized though.

I think I'm supposed to use events/listeners or something like that, but I'm not sure where to start looking.

edit: also, when i open one of the menus and it overlaps on the drawing, the region that was covered by the menus isn't updated.

Okay, let's try again: I have a class called draw. Then I have a GridBagLayout out of draws. This creates the grid I need. I have an object moving around the grid, so this way I can update the grid without having to calculate myself the rectangles that have to be updated.

Then this grid is inside a JScrollPane, which is not subclassed. I use pack() when I create my Window, but the thing is, the Window may be bigger than the resolution allows, so I need it to be scrollable. The scroll works for me, but it doesn't update the image when I use it. However, resizing the window does update it.

What I want is a way to redraw all my window when I click the JScrollPane. And now, I also need to redraw the whole thing (or some parts, I'll see that later) once I access the menus, because the draw parts of the grid under the menus are not updated.

That's the best way I can describe it.

A: 

Probably you need to specify the minimum size.

You don't post code or an screen shot so is hard to review it.

Try reading the comments in this thread, it look similar

http://stackoverflow.com/questions/448832/jscrollpane-now-showing-its-viewport

OscarRyz
A: 

It's been a while since I did any graphics in Swing, but I think the code you're looking for is something like:

Graphics g = getGraphics();
if (g != null) paintComponent(g);
else repaint();

And as you noted, you'll need to add this code in a listener attached to the JPanel's scroll functions.

I would probably create a Canvas that would contain the entire drawing and put it inside a JScrollPane. That will let Java do all the work!

Steve Moyer
+1  A: 

Just add a listener for scrolling viewport change events and issue a repaint(). I think I would advise against Steve Moyer's answer, since painting should only be done as part of a paint event.

And if the scrolled component is large enough you should just repaint the part of your JPanel which is visible within the viewport component (your JScrollPane).


Edit

To track the movement of the viewport, use JScrollPane getViewport() and JViewport addChangeListener(). Then use the methods of JViewport to identify what part of the view is visible when determining the region to paint in the painting method. Note that this should be an optimization applied only after the painting is working properly by repainting the entire view in each paint event.

Software Monkey
How can I tell what is currently visible?
See expanded answer
Software Monkey
I knew there had to be a better way than my method! I have removed my answer in favor of yours :)
coobird
A: 

Are you by any chance doing the painting of your "draw" outside the paint() or paintComponent() methods? If so you need to move your painting code into paintComponent().

jtplumber
A: 

It sounds like you want to do something like this:

    final JScrollPane pane = new JScrollPane();
    pane.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            pane.revalidate();
        }
    });

It may be a bit overkill, but every time you click on the scroll pane, it will attempt to repaint all the components in the hierarchy which should be similar to when you resize the screen.

Tony Eichelberger
A: 

make sure the panel the holding everything is set as the viewportview of your scroll pane (JScrollPane.setViewportView)

DW