Reputation: 188
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 JLayeredPane
doesn’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?
Upvotes: 3
Views: 2380
Reputation: 1417
What you are doing is adding the components control
and scroll
to 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 layeredPane
to:
layeredPane.add(scroll, 50, 0);
layeredPane.add(control, 51, 0);
This will place scroll
at position 0 on layer 50, and control
at position 0 on layer 51.
Upvotes: 2