0

Im trying to add a JScrollpane to my JPanel. The problem is that the scrollpane doesn't recognize that my drawing is outside the frame. So how do I add the JScrollpane correctly?

Main class:

public MainFrame() extends JFrame{

public MainFrame() {

        Container container = getContentPane(); 
        container(new BorderLayout());
        container.add(new JScrollPane(new Drawing()));

        setSize(1280,720);
        setVisible(true);
    }

Drawing class:

public class Drawing() extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
       g.drawLine(10, 100, 30000, 10);
    }
}
Korken55
  • 233
  • 1
  • 3
  • 14
  • For better help sooner post a proper [mcve], are you calling `pack()`? Are you extending `JFrame`? It's not a good idea: See [extends JFrame vs calling it inside of class](https://stackoverflow.com/questions/22003802/extends-jframe-vs-creating-it-inside-the-program) – Frakcool Nov 10 '17 at 18:10
  • My MainFrame extends JFrame and my Drawing class extends JPanel. I'm not calling pack(). – Korken55 Nov 10 '17 at 18:13
  • Did you read both links? A MCVE is needed, so we can help you faster and better – Frakcool Nov 10 '17 at 18:14
  • i extended the code, which reproduces my problem – Korken55 Nov 10 '17 at 18:22
  • 1
    Most probably, the problem is that your `Drawing` panel doesn't have the same size as your drawing `30000X10`. You'll have to specifically set its size, it won't be set automatically based on what you draw. – Titus Nov 10 '17 at 18:27

1 Answers1

1

There are a couple of errors in your code, let's step through each of them:

  1. You're extending JFrame, and you should avoid it, see: Extends JFrame vs. creating it inside the program for more information about it. You're actually not changing its behavior so it's not needed to extend it.

  2. For your JScrollPane to show the whole line, you need to change your window's size to be the same size of your line (as shown in this answer by @MadProgrammer).

  3. Related to point 2, avoid the use of setSize(...) and instead override getPreferredSize(): See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more information

  4. You forgot to call super.paintComponent(...) method in your paintComponent() method.

  5. Related to points 2, 3, you need to call pack() so Swing calculates the best preferred size for your component.

See this example:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class LongDraw {
    private JFrame frame;
    private Drawing drawing;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new LongDraw()::createAndShowGui);
    }

    private void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());

        drawing = new Drawing();

        JScrollPane scroll = new JScrollPane(drawing);

        frame.add(scroll);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    class Drawing extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            g2d.drawLine(10, 100, 3000, 10);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(3000, 500);
        }
    }
}

Which produces something similar to this:

enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89