This could be something very simple but I’m totally confused.
I have a JScrollPane inside a JLayeredPane when I scroll in the pane the things above in the JLayeredPanedoesn’t get repainted at all.
Here I got a small example notice how the blue square doesn’t get repainted at all.
Have I completely misunderstood how the layered pane works?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class LayeredTest extends JPanel{
public LayeredTest(){
JPanel content = new JPanel();
content.setBackground(Color.red);
content.setPreferredSize(new Dimension(2048, 2048));
content.setBounds(0, 0, 2048, 2048);
JPanel control = new JPanel();
control.setBackground(Color.blue);
control.setPreferredSize(new Dimension(200, 50));
control.setBounds(0, 0, 100, 50);
JScrollPane scroll = new JScrollPane(content);
scroll.setBounds(0, 0, 400, 400);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(400, 400));
layeredPane.add(control, 0);
layeredPane.add(scroll, 1);
this.add(layeredPane, BorderLayout.CENTER);
}
public static void main(String[] args) {
//Create and set up the window.
JFrame frame = new JFrame("Test - Very lulz");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.setSize(400, 400);
//Create and set up the content pane.
frame.setContentPane(new LayeredTest());
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
Any ideas?
What you are doing is adding the components
controlandscrollto the “default” layer, so in effect they are still on the same layer. To put them on different layers, you need to specify the layer number as well. It’s best to place the components somewhere between the default layer (which is the bottom-most with an index of 0) and the next layer, which is the “palette” layer with an index of 100.So, to put the components on layers 50 and 51, for example, change where you’re adding the components to
layeredPaneto:This will place
scrollat position 0 on layer 50, andcontrolat position 0 on layer 51.