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

The Archive Base Latest Questions

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

I need to handle event from JScrollPane when user scrolls it. In here it

  • 0

I need to handle event from JScrollPane when user scrolls it. In here it is suggested to use addAdjustmentListener to either JViewport or Vertical/HorizontalScrollBar.

Is there any difference, which method should I choose?

I would like also to use scrollRectToVisible for the same JScrollPane, should I anticipate AdjustmentListener working for this method?

Also I wonder whether scrollRectToVisible tries to do minimum scrolling to make required rectangular visible or it tries to make it visible in the middle of JViewport ?

UPD: Requirements:

1) There a JScrollPane with one JPanel which has many JLabel(ImageIcon), so some of them are not visible.

2) When a network event comes I need to show one of JLabel (make it visible) to user. If I is not visible originally then JScrollPane should scroll automatically. That’s why I mention scrollRectToVisible.

3) Above that JLabel with ImageIcon inside I need to show a message which would explain what has happened to this element. The message currently is implemented as another JLabel with floats thanks to JLayeredPane much higher in hierarchy. The problem now is that if user scrolls JScrollPane that floating JLabel should move accordingly to be on top of correponding JLabel(ImageIcon).

UDP2: SSCCE

I have not implemented floating JLabel yet, but already I don’t like how label with index 11 reacts to scrollRectToVisible since it is half cropped.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class JScrollPaneTest {
    protected ArrayList<JLabel> labels = new ArrayList<JLabel>();

    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() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                JPanel panel1 = new JPanel ();

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

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

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

                JScrollPane pane = new JScrollPane(panel1) {
                    private static final long serialVersionUID = 1L;

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

                pane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
                    @Override
                    public void adjustmentValueChanged(AdjustmentEvent e) {
                        System.out.println("adjustmentValueChanged: " + e);
                    }
                });

                frame.getContentPane().add(pane);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
  • 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:12:06+00:00Added an answer on June 15, 2026 at 5:12 am

    Part of the answer (code in comments is just … and noticed I had it wrong in the comment 😉

    The method scrollRectToVisible takes a Rectangle in the coordinate system of the component it is called upon. To scroll a child such that it is completely visible, there are basically two options (use one of them):

    // either call on child with zero offset
    child.scrollRectToVisible(new Rectangle(child.getSize());
    // or call on child's parent with child bounds
    child.getParent().scrollRectToVisible(child.getBounds());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to handle exception in Application_UnhandledException event of App.xaml.cs file. I am getting
I need a simple example of a Tunnel Routed Event tunnelling from a parent
I need to play a youtube video in UIWebview and handle the event when
I need to pass the touches and event from touchesBegan to my own method
I am trying to handle a drop event on a TreeWidget from itself by
A) Book I’m learning from says that if we handle Login.Authenticate event, then we
I need to handle events of two different types but I'm running into the
In my image's onLoad event handler I need to fetch the URL of the
In the AfterPost event handler for a ClientDataSet, I need the information if the
I cannot handle properly some events in input type=text . I just need a

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.