Reputation: 303
I got an unexpected behavior on my JScrollPane: My ScrollPanel is filled with different panels (Transparency is needed because in the end I will have images in background instead of just colors)
I made a quick example of my problem:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.border.Border;
public class ScrollPaneTest extends JPanel{
ScrollPaneTest(){
JPanel Content = new JPanel();
BoxLayout ColumnLayout = new BoxLayout(Content,BoxLayout.Y_AXIS);
Content.setLayout(ColumnLayout);
for(int i = 0 ; i < 10 ; i++){
JPanel pane = new JPanel();
JLabel elem = new JLabel("element "+i);
pane.setBackground(new Color(0,0,0,125));
pane.add(elem);
Content.add(pane);
}
Content.setBackground(Color.ORANGE);
JScrollPane scrollPane = new JScrollPane(Content);
scrollPane.setPreferredSize(new Dimension(100,100));
scrollPane.setBackground(new Color(0,0,0,250));
add(scrollPane);
}
public static void main(String[] args) throws Exception {
JFrame f = new JFrame();
JPanel bck = new JPanel();
bck.setBackground(Color.RED);
bck.add(new ScrollPaneTest());
f.add(bck);
f.setSize(200, 200);
f.setVisible(true);
}
}
With this you can see that when I scroll, the graphics are all messed up :/ Thanks for Helping :p
Upvotes: 0
Views: 611
Reputation: 7324
I have the Following Solution for your Problem
When you scroll through the elements the elements are not repainted so you need to repaint them when you scroll the scroll bar. Add the following code after this line JScrollPane scrollPane = new JScrollPane(Content);
it will work!
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
repaint();
}
});
Upvotes: 2