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 8916743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:19:13+00:00 2026-06-15T05:19:13+00:00

There are 20 JLabel s inside JScrollPane . There is also floatingLabel added to

  • 0

There are 20 JLabels inside JScrollPane. There is also floatingLabel added to JLayeredPane together with JscrollPane mentioned above.

The requirement is that when I click inside JScrollPane it should scroll automatically to make JLabel with index 11 completely visible. Also floatingLabel should start floating above that JLabel 11 when a user scrolls JScrollPane, refer to AdjustmentListener.

Issues [EDIT]:
1. How to check that JLabel 11 becomes completely or partially invisible?

SSCCE:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class JScrollPaneTest {
    protected ArrayList<JLabel> labels = new ArrayList<JLabel>();
    protected JLabel floatingLabel = new JLabel("floating");
    protected JFrame frame;
    protected JScrollPane sPane;
    protected JLayeredPane lPane;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JScrollPaneTest();
            }
        });
    }

    public JScrollPaneTest() {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                lPane = new JLayeredPane() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(800, 600);
                    }
                };

                JPanel panel = new JPanel ();

                panel.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();

                for (int i = 0; i < 20; i++) {
                    JLabel label = new JLabel("  | Label" + i + " |  ");
                    panel.add(label, gbc);
                    labels.add(label);
                }

                panel.addMouseListener(new MouseAdapter(){
                    public void mousePressed (MouseEvent me) {
                        JLabel label = labels.get(11);
                        label.scrollRectToVisible(label.getBounds());

                        Point loc = label.getLocation();
                        Point wrPos = SwingUtilities.convertPoint(label, loc, lPane);
                        floatingLabel.setLocation(wrPos);
                        floatingLabel.setVisible(true);
                    }
                });

                sPane = new JScrollPane(panel) {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(600, 400);
                    }
                };

                sPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
                    @Override
                    public void adjustmentValueChanged(AdjustmentEvent ae) {
                        System.out.println("adjustmentValueChanged: " + ae.getValue());

                        //if label11 is not visible
                        //floatingLabel.setVisible(false);
                        //else floatingLabel.setLocation();
                    }
                });

                lPane.add(sPane, JLayeredPane.PALETTE_LAYER);
                sPane.setBounds(100, 100, (int)sPane.getPreferredSize().getWidth(), (int)sPane.getPreferredSize().getHeight());

                lPane.add(floatingLabel, JLayeredPane.POPUP_LAYER);
                floatingLabel.setBounds(100, 100, 50, 30);
                floatingLabel.setBorder(BorderFactory.createLineBorder(Color.red));

                frame.getContentPane().add(lPane);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

Solved issues:

  1. JLabel 11 is not completely visible after click, it is half cropped
  2. I fail to calculate proper location for floatingPanel on click
  3. I don’t know how to calculate new location on scrolling
  • 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-06-15T05:19:14+00:00Added an answer on June 15, 2026 at 5:19 am
    label.scrollRectToVisible(new Rectangle(0,0,label.getWidth(), label.getHeight()));
    

    Use this instead in the click

    public void mousePressed (MouseEvent me) {
      JLabel label = labels.get(11);
      label.scrollRectToVisible(new Rectangle(0,0,label.getWidth(), label.getHeight()));
    
      //Point loc = label.getLocation();
      Point loc = new Point(0,0);
      Point wrPos = SwingUtilities.convertPoint(label, loc, lPane);
      floatingLabel.setLocation(wrPos);
      floatingLabel.setVisible(true);
    }
    
    public void adjustmentValueChanged(AdjustmentEvent ae) {
      System.out.println("adjustmentValueChanged: " + ae.getValue());
    
      JLabel label = labels.get(11);
    
      Point loc = new Point(0,0);
      Point wrPos = SwingUtilities.convertPoint(label, loc, lPane);
      floatingLabel.setLocation(wrPos);
      floatingLabel.setVisible(true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a widget for Swing that behaves like a JLabel , that automatically
I know there is a way to extend a JLabel to paint 3D borders
I have a JLabel which has a lot of text on it. Is there
I am trying to reset an array of JLabels. There are images on the
There is a moment in my app, that I need to force to show
There is a column that exists in 2 tables. In table 1, this column
I am working on a program that has a flow layout, inside are a
I have a JFrame window with GridBagLayout(). There is a JLabel with Icon -
I have a main JFrame. There is a button inside the frame. When I
I've a class called MapTile that extends JLabel . What I'm doing is loading

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.