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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:11:43+00:00 2026-06-14T08:11:43+00:00

I have modified a little code from here: https://stackoverflow.com/a/13357269/1360074 Since there is known bug

  • 0

I have modified a little code from here: https://stackoverflow.com/a/13357269/1360074

Since there is known bug with JScrollPane not passing MouseEvent up I did a workaround with FakeMouseListener.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class LostMouseEvent {
    private JPanel panel1;

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
    new LostMouseEvent();
        }
    });
}

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

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

            };
            JPanel panel2 = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(500, 300);
                }
            };
            JScrollPane pane = new JScrollPane(panel2);


            panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
            panel2.setBorder(BorderFactory.createLineBorder(Color.green));

            panel1.setLayout(new CircleLayout());


            panel1.add(pane);
            frame.add(panel1);

            MouseListener rml = new RealMouseListener();
            panel1.addMouseListener(rml);

            MouseListener fml = new FakeMouseListener();
            pane.addMouseListener(fml);

            frame.pack();
            frame.setVisible(true);

        }
    });
}

private class RealMouseListener extends MouseAdapter {
    @Override
    public void mousePressed(MouseEvent me) {
        System.out.println(me);

        Point point = me.getPoint();

        System.out.println(panel1.getComponentAt(point));
        System.out.println(panel1.getComponent(0));
    }
}

private class FakeMouseListener extends MouseAdapter {
     @Override
     public void mousePressed(MouseEvent me) {
         panel1.dispatchEvent(me);
     }
}
}

Now if click inside green border on the left side just next to border I get:

java.awt.event.MouseEvent[MOUSE_PRESSED,(9,169),absolute(66,248),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on javax.swing.JScrollPane[,49
LostMouseEvent$2$1[,0,0,600x400,layout=CircleLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@633d51
javax.swing.JScrollPane[,49,49,503x303,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,

If I click in the middle I get:

java.awt.event.MouseEvent[MOUSE_PRESSED,(247,147),absolute(304,226),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on javax.swing.JScrollPane[,
    javax.swing.JScrollPane[,49,49,503x303,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.metal.
    javax.swing.JScrollPane[,49,49,503x303,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.metal.

Why is that? Do you I need to modify coordinates of MouseEvent when I re-dispatch it?

Code for CircleLayout is

    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.LayoutManager;


    public class CircleLayout implements LayoutManager {
        private int heightGap;

    public CircleLayout () {
        heightGap = 0;
    }

    public CircleLayout (int heightGap) {
        this.heightGap = heightGap;
    }

    /**
    * Arranges the parent's Component objects in either an Ellipse or a Circle.
    * Ellipse is not yet implemented.
    */
    public void layoutContainer (Container parent) {
        int x, y, w, h, s, c;
        int childCompNum = parent.getComponentCount();

        int parentWidth = (int)parent.getSize().width;
        int parentHeight = (int)parent.getSize().height;

        int centerX = (int) (parentWidth / 2);
        int centerY = (int) (parentHeight / 2);

        double angleOffset = 0.5 * Math.PI;

        Component childComp = null;
        for (int i = 0; i < childCompNum; i++) {
            childComp = parent.getComponent(i);
            w = childComp.getPreferredSize().width;
            h = childComp.getPreferredSize().height;

            if (childCompNum == 1) {
                x = centerX - (int)w / 2;
                y = centerY - (int)h / 2;
            } else {
                c = (int) (centerX * Math.cos((2 * i * Math.PI + angleOffset) / childCompNum));
                s = (int) (centerY * Math.sin((2 * i * Math.PI + angleOffset) / childCompNum));

                x = c + centerX - (int)w / 2;
                y = s + centerY - (int)h / 2;

                if (x + w  > parentWidth) {x = (int)(parentWidth - w); }
                if (y + h + heightGap > parentHeight) {y = (int)(parentHeight - h -heightGap); }
                if (x < 0) {x = 0; }
                if (y < 0) {y = 0; }
            }

            childComp.setBounds(x, y, w, h);
        }
    }

    /** For compatibility with LayoutManager interface */
    public void addLayoutComponent (String name, Component comp) {}

    public Dimension preferredLayoutSize(Container target) {
        return target.getSize();
    }

    public Dimension minimumLayoutSize(Container target) {
        return target.getSize();
    }

    public void removeLayoutComponent(Component comp) {}
}

UPD: I have modified FakeMouseListener to

     public void mousePressed(MouseEvent me) {
         JScrollPane pane = (JScrollPane)me.getSource();
         MouseEvent newMe = SwingUtilities.convertMouseEvent(pane, me, pane.getViewport());
         panel1.dispatchEvent(me);
     }

But have same behavior.

  • 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-14T08:11:44+00:00Added an answer on June 14, 2026 at 8:11 am

    code

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    
    public class LostMouseEvent {
    
        private JPanel panel1;
        private JPanel panel2 = new JPanel();
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    LostMouseEvent lostMouseEvent = new LostMouseEvent();
                }
            });
        }
    
        public LostMouseEvent() {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    panel1 = new JPanel() {
    
                        private static final long serialVersionUID = 1L;
    
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(600, 400);
                        }
                    };
                    panel2 = new JPanel() {
    
                        private static final long serialVersionUID = 1L;
    
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(500, 300);
                        }
                    };
                    JScrollPane pane = new JScrollPane(panel2);
                    panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
                    panel2.setBorder(BorderFactory.createLineBorder(Color.green));
                    panel1.setLayout(new CircleLayout());
                    panel1.add(pane);
                    frame.add(panel1);
                    MouseListener rml = new RealMouseListener();
                    panel1.addMouseListener(rml);
                    MouseListener fml = new FakeMouseListener();
                    panel2.addMouseListener(fml);
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    
        private class RealMouseListener extends MouseAdapter {
    
            @Override
            public void mousePressed(MouseEvent me) {
                Rectangle rec = SwingUtilities.convertRectangle(panel2, panel2.getVisibleRect(), panel1);
                System.out.println(me);
                Point point = me.getPoint();
                System.out.println(panel1.getComponentAt(point));
                System.out.println(panel1.getComponent(0));
            }
        }
    
        private class FakeMouseListener extends MouseAdapter {
    
            @Override
            public void mousePressed(MouseEvent me) {
                JScrollPane pane = (JScrollPane) me.getSource();
                MouseEvent newMe = SwingUtilities.convertMouseEvent(pane, me, pane.getViewport());
                panel1.dispatchEvent(me);
            }
        }
    }
    

    returns

        Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:  AAA_Format.LostMouseEvent$2$2 cannot be
    

    cast to javax.swing.JScrollPane
    at AAA_Format.LostMouseEvent$FakeMouseListener.mousePressed(LostMouseEvent.java:95)
    at java.awt.Component.processMouseEvent(Component.java:6264)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6032)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4235)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    EDIT

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    
    public class LostMouseEvent {
    
        private JPanel panel1;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new LostMouseEvent();
                }
            });
        }
    
        public LostMouseEvent() {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    panel1 = new JPanel() {
                        private static final long serialVersionUID = 1L;
    
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(600, 400);
                        }
                    };
                    JPanel panel2 = new JPanel() {
                        private static final long serialVersionUID = 1L;
    
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(500, 300);
                        }
                    };
                    JScrollPane pane = new JScrollPane(panel2);
                    panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
                    panel2.setBorder(BorderFactory.createLineBorder(Color.green));
                    panel1.setLayout(new CircleLayout());
                    panel1.add(pane);
                    frame.add(panel1);
                    MouseListener rml = new RealMouseListener();
                    panel1.addMouseListener(rml);
                    MouseListener fml = new FakeMouseListener();
                    pane.addMouseListener(fml);
                    frame.pack();
                    frame.setVisible(true);
    
                }
            });
        }
    
        private class RealMouseListener extends MouseAdapter {
    
            @Override
            public void mousePressed(MouseEvent me) {
                System.out.println(me);
                Point point = me.getPoint();
                System.out.println(me.getX());
                System.out.println(me.getXOnScreen());
                System.out.println(me.getY());
                System.out.println(me.getYOnScreen());
            }
        }
    
        private class FakeMouseListener extends MouseAdapter {
    
            @Override
            public void mousePressed(MouseEvent me) {
                JScrollPane pane = (JScrollPane) me.getSource();
                MouseEvent newMe = SwingUtilities.convertMouseEvent(pane.getViewport(), me, panel1);
                System.out.println(newMe.getX());
                System.out.println(newMe.getXOnScreen());
                System.out.println(newMe.getY());
                System.out.println(newMe.getYOnScreen());
                panel1.dispatchEvent(me);
            }
        }
    }
    

    EDIT 2

    more logical could be replace inside FakeMouseListener, from

    JScrollPane pane = (JScrollPane) me.getSource(); 
    MouseEvent newMe = SwingUtilities.convertMouseEvent(pane.getViewport(), me, panel1);
    

    to

    JPanel panel2 = (JPanel) me.getSource(); 
    MouseEvent newMe = SwingUtilities.convertMouseEvent(panel2, me, panel1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have assembly A. It was modified with Mono.Cecil a little bit. Now
Recently I have modified my code to While taking input form STDIN, I moved
I have got this little snippet of code, I want to be able to
well, the title pretty much says it all. here's a little info: i have
I have code listed here: Threading and Sockets . The answer to that question
I have modified a slider script to mimic ebay's slider on home page. I
I have modified the Nerd Dinner application to allow editing of child records by
I have modified my MOSS 2007 configuration to query a given target AD successfully.
I have modified a working Windows service that had always been starting beforehand. After
I have modified a website with a redirection to a single page: RewriteCond %{REQUEST_FILENAME}

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.