1

enter image description here

I have a JPanel inside the JScrollPane and it does that whenever I try to scroll. Please help! How do I fix this?

EDIT

JScrollPane pane;
....
pane = new JScrollPane(GC.createGraph());
pane.setPreferredSize(new Dimension(480,480*2/3));
if_zero_equals_one
  • 1,716
  • 4
  • 17
  • 30
  • You've got a bug in your code that paints your held JPanel, for instance perhaps a missing call to super.paintComponent(...) or super.paint(...), but, but without code, I'm not sure how we can help you. Please consider creating and posting an [sscce](http://sscce.org) – Hovercraft Full Of Eels Aug 02 '11 at 21:12
  • What exactly is happening? Even if you scroll 100% you have content hidden below? to right or to bottom? – ring bearer Aug 02 '11 at 21:14
  • There is no code after throwing a functioning JPanel into a JScrollPane the scrollPane creates artifacts when I try to scroll. And the picture (full) is supposed to be a large thin X. – if_zero_equals_one Aug 02 '11 at 21:18
  • There's always code -- how do you draw this large thin X? Are you doing this in the JPanel's paintComponent method? **Again** are you calling the super method? – Hovercraft Full Of Eels Aug 02 '11 at 21:23
  • ...somedays I wonder why I program...Thanks. – if_zero_equals_one Aug 02 '11 at 21:27

2 Answers2

3

Placing as an answer for others to see. If you don't call the super.paintComponent, you'll get those artifacts. e.g.,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.*;

public class ScrollPaneArtifacts extends JPanel {
   private static final int SPA_WIDTH = 600;
   private static final int SPA_HEIGHT = SPA_WIDTH;

   @Override
   protected void paintComponent(Graphics g) {
      //super.paintComponent(g);
      g.setColor(Color.red);
      g.drawLine(0, 0, getWidth(), getHeight());
      g.drawLine(getWidth(), 0, 0, getHeight());
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(SPA_WIDTH, SPA_HEIGHT);
   }

   private static void createAndShowUI() {
      JScrollPane scrollpane = new JScrollPane(new ScrollPaneArtifacts());
      scrollpane.getViewport().setPreferredSize(new Dimension(400, 400));
      JFrame frame = new JFrame("ScrollPaneArtifacts");
      frame.getContentPane().add(scrollpane);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

You do not need to call super.paintComponent you can simply clear the area to ensure no artifacts are left on the panel from the previous render (which calling super.paintComponent will do).

@Override
protected void paintComponent(Graphics g) {
  g.clearRect(0,0,getWidth(),getHeight());
  g.setColor(Color.red);
  g.drawLine(0, 0, getWidth(), getHeight());
  g.drawLine(getWidth(), 0, 0, getHeight());
}

Try this in Hovercrafts code if you like.

Adrian
  • 495
  • 2
  • 10