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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:50:24+00:00 2026-06-07T16:50:24+00:00

I have a JScrollPane that can set its viewportView to a range of different

  • 0

I have a JScrollPane that can set its viewportView to a range of different panels. I want to get the JScrollPane component whenever any other component in its viewport is clicked. If I add a MouseListener to the JScrollPane, it receives my mouse events when I click directly on the border of the pane, but not when I click on the components.

What’s the right way to go about adding listeners and ultimately finding the enclosing scrollPane? I won’t necessarily know ahead of time all the components on the panel that I show in the viewport – just that they’ll be on some subclass of JPanel.

import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.MouseInputAdapter;

import net.miginfocom.swing.MigLayout;

public class TestScrollPane extends MouseInputAdapter{
    public void mouseEntered(MouseEvent arg0) {System.out.println("Entered " + arg0.getComponent());}
    public void mouseExited(MouseEvent arg0) {System.out.println("Exited " + arg0.getComponent());}
    public void mousePressed(MouseEvent arg0) {System.out.println("Pressed " + arg0.getComponent());}
    public void mouseReleased(MouseEvent arg0) {System.out.println("Released " + arg0.getComponent());}

    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setLayout(new MigLayout());
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TestPane pane1 = new TestPane("Scroll Pane 1");
        TestPane pane2 = new TestPane("Scroll Pane 2");
        frame.add(pane1, "push,grow");
        frame.add(pane2, "push, grow");
        TestMouseListener listener = new TestMouseListener();
        pane1.addMouseListener(listener);
        pane1.addMouseMotionListener(listener);
        pane2.addMouseListener(listener);
        pane2.addMouseMotionListener(listener);
        frame.setVisible(true);
    }
}

class TestPanel2 extends JPanel {
    String name;
    TestPanel2(String name){ 
        this.name = name;
        setLayout(new MigLayout());
        JTextArea area = new JTextArea();
        area.append(name);
        add(area, "push, grow");
    }
    public String toString(){ return name; }
}
class TestPane extends JScrollPane {
    String name;
    TestPane(String name){ 
        this.name = name; 
        TestPanel2 panel = new TestPanel2(name + " panel");
        setViewportView(panel);
    }
    public String toString(){ return name; }
}

In this example, I get mouse enter and exit events, but I can only get the mouse clicked event by clicking on the border around the text area. Even if I change the TestPane class to add listeners to its viewportView panel, I can’t tell what’s going on in the textArea.

class TestPane extends JScrollPane {
    String name;
    TestPane(String name){ 
        this.name = name; 
        TestPanel2 panel = new TestPanel2(name + " panel");
        TestMouseListener listener = new TestMouseListener();
        panel.addMouseListener(listener);
        panel.addMouseMotionListener(listener);
        setViewportView(panel);
    }
    public String toString(){ return name; }
}

I won’t have any way of knowing what’s on the JPanel, though, so I can’t manually add listeners any deeper.

  • 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-07T16:50:26+00:00Added an answer on June 7, 2026 at 4:50 pm

    Another possible way is to use an AWTEventListener, and then bubble up the parent tree to see if your component of interest has been pressed or holds a child that has been pressed. For example:

    import java.awt.AWTEvent;
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.Toolkit;
    import java.awt.event.AWTEventListener;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.event.MouseInputAdapter;
    
    // import net.miginfocom.swing.MigLayout;
    
    public class TestScrollPane extends MouseInputAdapter {
       public void mouseEntered(MouseEvent arg0) {
          System.out.println("Entered " + arg0.getComponent());
       }
    
       public void mouseExited(MouseEvent arg0) {
          System.out.println("Exited " + arg0.getComponent());
       }
    
       public void mousePressed(MouseEvent arg0) {
          System.out.println("Pressed " + arg0.getComponent());
       }
    
       public void mouseReleased(MouseEvent arg0) {
          System.out.println("Released " + arg0.getComponent());
       }
    
       public static void main(String[] args) {
          JFrame frame = new JFrame();
          // frame.setLayout(new MigLayout());
          frame.getContentPane().setLayout(new FlowLayout());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          TestPane pane1 = new TestPane("Scroll Pane 1");
          TestPane pane2 = new TestPane("Scroll Pane 2");
          frame.add(pane1, "push,grow");
          frame.add(pane2, "push, grow");
          // !! TestMouseListener listener = new TestMouseListener();
          TestScrollPane listener = new TestScrollPane();
          pane1.addMouseListener(listener);
          pane1.addMouseMotionListener(listener);
          pane2.addMouseListener(listener);
          pane2.addMouseMotionListener(listener);
          frame.pack();
          frame.setVisible(true);
    
          Toolkit.getDefaultToolkit().addAWTEventListener(
                listener.createAWTWindowListener(), AWTEvent.MOUSE_EVENT_MASK);
    
       }
    
       private AWTEventListener createAWTWindowListener() {
          AWTEventListener awt1 = new AWTEventListener() {
    
             @Override
             public void eventDispatched(AWTEvent e) {
                if (MouseEvent.MOUSE_PRESSED == e.getID()) {
                   MouseEvent event = (MouseEvent) e;
                   Component comp = event.getComponent();
    
                   if (comp != null) {
                      String scrollPanelName = recursivelyCheckForScrollPanel(comp);
                      if (scrollPanelName != null) {
                         System.out.println("TestPane pressed. Name: " + scrollPanelName);
                      } else {
                         System.out.println("TestPane not pressed");
                      }
                   }
                }
             }
    
             private String recursivelyCheckForScrollPanel(Component comp) {
                if (comp instanceof TestPane) {
                   return comp.toString();
                } else {
                   comp = comp.getParent();
                   if (comp != null) {
                      return recursivelyCheckForScrollPanel(comp);
                   }
                }
                return null;
             }
          };
          return awt1;
       }
    }
    
    class TestPanel2 extends JPanel {
       String name;
    
       TestPanel2(String name) {
          this.name = name;
          // setLayout(new MigLayout());
          JTextArea area = new JTextArea(5, 20);
          area.append(name);
          add(area, "push, grow");
       }
    
       public String toString() {
          return name;
       }
    }
    
    class TestPane extends JScrollPane {
       String name;
    
       TestPane(String name) {
          this.name = name;
          TestPanel2 panel = new TestPanel2(name + " panel");
          setViewportView(panel);
       }
    
       public String toString() {
          return name;
       }
    }
    

    Note: Please see this question and StanislovL’s and mkorbel’s answers for more on this.

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

Sidebar

Related Questions

I have a JScrollPane with FlowLayout that I want to have a fixed width.
I have a JScrollpane which contains a scrollable client that changes its size dynamically
I have a JScrollPane with a JPanel that implements Scrollable as its viewport view.
I'm making a custom ListCellRenderer. I know that you can have different dimensions for
I have on my page 2 DIVs that uses jScrollPane and I want each
Somehow, i don't get it how i can set the height of jScrollPane. http://tinyw.in/BLrg
I have a simple JEditorPane inside a JScrollPane that displays line numbers on the
I have embedded a JTextArea on a JScrollPane and am using that JTextArea for
Anyone have any luck getting JScrollPane to work with JQuery? I'm following the instructions
I have a customText component in a JScrollPane. When the text is empty and

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.