0

Intro

I created a class ChatView that extends JPanel. I added it as a parameter to my JScrollPane (to get the scrolling effect) and overrode the paintComponent method in hope of setting a background image:

new JScrollPane(new ChatView){
            @Override
            protected void paintComponent(Graphics g) {

                super.paintComponent(g);
                ImageIcon background = new ImageIcon("files/background.png");
                Image image = background.getImage();
                image.getScaledInstance(980, 600, java.awt.Image.SCALE_SMOOTH);
                if (image != null)
                {
                    g.drawImage(image,0,0,this.getWidth(),this.getHeight(),null);
                }

            }
        }

problem

This doesn't seem to do anything; however, the same method works on my class ChatView, which extends JPanel. the problem with adding it to my JPanel is that it stretches every time I add a new component that extends outside of the setPrefferredSize() of my JScrollPane; If I do not revalidate() and repaint() my JPanel, it cuts off or doesn't add the components. That's why I think it is necessary to add it to the JScrollPane, but how? or am i on the wrong track?

Code: attributes of my JScrollPane

public void addScroll(JScrollPane pnl, String str, int w, int h){
        pnl.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        pnl.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        pnl.setSize(new Dimension(w, h));
        pnl.setMinimumSize(new Dimension(w, h));
        pnl.setPreferredSize(new Dimension(w, h));
        pnl.getVerticalScrollBar().setUnitIncrement(16);
        pnl.setBorder(BorderFactory.createLineBorder(black));
        this.add(pnl, str);
    }
camickr
  • 321,443
  • 19
  • 166
  • 288
hadsag
  • 11
  • 3
  • Your panel needs to have a "preferred size" for it to be successful presented. At the movement, it's `0x0`, so the `JScrollPane` is making `0x0` when it's presented – MadProgrammer Feb 22 '22 at 01:09
  • You might like to have a look at [How to set a background picture in JPanel](https://stackoverflow.com/questions/22162398/how-to-set-a-background-picture-in-jpanel/22162430#22162430) and because you've thrown a `JScrollPane` into the mix, [Java - Is there a way to insert a background image in a JList?](https://stackoverflow.com/questions/16390918/java-is-there-a-way-to-insert-a-background-image-in-a-jlist/16390963#16390963) – MadProgrammer Feb 22 '22 at 01:13
  • I setPreferredSize() on my JPanel, but then, it won't add any new components outside of its preferredSize. The JPanel need to be extendable so this method won't work! I have setMinimumSize(new Dimension(980,600)) to the JPanel, but no max or preferred size – hadsag Feb 22 '22 at 01:53
  • @MadProgrammer I setOpaque(false) on both, as suggested in the linked post, but to no avail. I have a preferred size on my JScrollPane and i set the image to the exact same dimension, but it doesn't paint the image. setBackground(Black) doesn't even work. – hadsag Feb 22 '22 at 02:05
  • 1. Don't use `ImageIcon`, use `ImageIO.read`, this throws an error when the image can't be loaded; 2. Don't load images in `paintComponent`, this will be a source of bad performance from within your application. I've provided, at least 2, possibly more runnable examples, consider providing a [mcve] to help remove an ambaility – MadProgrammer Feb 22 '22 at 02:19
  • You need to draw the image on the viewport of the scroll pane. Checkout out: https://stackoverflow.com/questions/68474883/static-image-behind-jtextpane-text-in-jscrollpane/68476235#68476235 – camickr Feb 22 '22 at 05:37

1 Answers1

0

Static Image behind JTextPane text in JScrollPane

^^link where I found the solution!! Below is a clean cheatsheet :)

JPanel jPanel = new JPanel() {
    this.setOpaque(false);
};
JViewport viewport = new JViewport() {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ImageIcon background = new ImageIcon("images/background.png");
        Image image = background.getImage();
        image.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
        if (image != null) {
            g.drawImage(image, 0, 0, w, h, null);
        }
    }
};
jScrollPane.setViewport(viewport);
jScrollPane.setViewportView(jPanel);
ThrowsError
  • 1,169
  • 1
  • 11
  • 43
hadsag
  • 11
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 22 '22 at 17:00