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);
}