Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6382383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:30:54+00:00 2026-05-25T02:30:54+00:00

This could be something very simple but I’m totally confused. I have a JScrollPane

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:30:55+00:00Added an answer on May 25, 2026 at 2:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I could do this myself given time but does anyone have a nice asp.net
Hopefully this is very simple. I have a central activity that can be launched
I searched for a sample for this, but could not find something that explains
I'm sure this is something simple but I am just not seeing it. My
This could be weird, Have you ever come across a blog which you wanted
This could be a little off the ballpark, but a friend asked me about
I have a lot of classes in the App_Code directory could this be a
I have roughly the following code. Could this be made nicer or more efficient?
What I want to do is very simple but I'm trying to find the
I have a very simple queue implementation that wraps a fixed array. It contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.