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

  • Home
  • SEARCH
  • 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 8821107
In Process

The Archive Base Latest Questions

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

This is the code I am using to show the issue which I am

  • 0

This is the code I am using to show the issue which I am facing in another project.

I am not getting any line like this if I use JScrollPane as a wrapper for panel2. Why?
I want to click on JscrollPane and got event printed as following.

java.awt.event.MouseEvent[MOUSE_CLICKED,(800,469),absolute(808,499),button=1,modifiers=Button1,clickCount=1] on javax.swing.JPanel[,0,0,934x612,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@cc0e01,flags=9,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=880,height=630]]

If now I change

panel1.add(pane);

to

panel1.add(panel2);

Then the message above got printed.

public class LostMouseEvent {

public static void main(String[] args) {
    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());

            JPanel panel1 = new JPanel();
            JPanel panel2 = new JPanel();
            JScrollPane pane = new JScrollPane(panel2);

            panel1.setPreferredSize(new Dimension(880, 630));
            panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
            panel2.setPreferredSize(new Dimension(840, 610));
            panel2.setBorder(BorderFactory.createLineBorder(Color.green));


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

            frame.pack();
            frame.setVisible(true);
            frame.setSize(950, 650);

            panel1.addMouseListener(new MyMouseListener());
        }
    });
}

private class MyMouseListener extends MouseAdapter {
    @Override
    public void mouseClicked (MouseEvent me) {
        System.out.println(me);
    }
}
}

UPD: In fact in my project there is more than just one panel2. Originally, I had panel1 and many panel2 inside. Then I wanted to wrap each panel2 with JScrollPane and started to face this problem.

I need to have only one MouseListener to minimize changes to the code.

  • 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-14T05:46:20+00:00Added an answer on June 14, 2026 at 5:46 am
    • Use EDT for creation and manipulation of Swing components
    • Dont call setSize() rather call pack() before setting JFrame visible.
    • Dont call setPrefferedSize() rather override getPrefferedSize()

    Your code works as expected, it will only print the message if panel1 is clicked, note panel1 is behind JScrollPane, thus anything outside the green border is panel1. To make it work for both the JScrollpane/panel2 and JPanel/panel1 simply add the MouseListener to BOTH of the required components:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    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 {
    
        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());
    
                    JPanel panel1 = new JPanel() {
    
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(880, 630);
                        }
    
                    };
                    JPanel panel2 = new JPanel() {
    
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(840, 610);
                        }
                    };
                    JScrollPane pane = new JScrollPane(panel2);
    
                    panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
                    panel2.setBorder(BorderFactory.createLineBorder(Color.green));
    
    
                    panel1.add(pane);
                    frame.add(panel1);
    
                    MouseListener ml=new MyMouseListener();
    
                    //add mouse listener to panel1 and panel2
                    panel1.addMouseListener(ml);
                    panel2.addMouseListener(ml);
    
                    //alternatively add to pane
                    //pane.addMouseListener(ml);
    
                    frame.pack();
                    frame.setVisible(true);
    
                }
            });
        }
    
        private class MyMouseListener extends MouseAdapter {
    
            @Override
            public void mouseClicked(MouseEvent me) {
                System.out.println(me);
            }
        }
    }
    

    EDIT:

    I personally would not recommend this, however,

    To add a single listener to the JFrame that will capture all MouseEvents use Toolkit class and call addAWTEventListener like so:

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        @Override
        public void eventDispatched(AWTEvent awte) {//all mouse events will be processed here you will have to check for the mouse events you are interested in
        System.out.println(awte);
        }
    }, AWTEvent.MOUSE_EVENT_MASK);//for Mouse events only
    

    UPDATE 1:

    You could also add the MouseListener to your JFrames glasspane via JFrame.getGlassPane().addMouseListener(ml) dont forget to set the glasspane visible after setting JFrame visible. This will allow you to only have to add a single Listener. See here:

    ...
    
    MouseListener ml = new MyMouseListener();
    
    //add mouse listener to panel1 and panel2
    //panel1.addMouseListener(ml);
    //panel2.addMouseListener(ml);
    
    //alternatively add to pane
    //pane.addMouseListener(ml);
    
    frame.getGlassPane().addMouseListener(ml);
    
    frame.pack();
    frame.setVisible(true);
    
    frame.getGlassPane().setVisible(true);
    
    ...
    

    UPADTE 2:

    The main reason for you having the problem of the MouseEvent getting lost in JScrollPane is because its a bug. See here.

    The work around shown is:

    public Test()
      {
        setUI(new javax.swing.plaf.metal.MetalScrollPaneUI(){
          public void installListeners(JScrollPane scrollPane){}
        });
        JPanel canvas = new JPanel();
        canvas.add( new JLabel("Test") );
    
        setViewportView( canvas );
        setVisible(true);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using this code to show/hide a div: <a href=# class=show_hide>Show/hide</a> <div class=slidingDiv> My
I am using this code in my website to show Google search results for
I'm currently using this code (per the codex) to show children on parent pages,
After using this code to get my link to show me 3 database entrys
I am using this code to parse this date. It must show new date
I have this code to show a map using the Virtual Earth API: <script
I'm using this jquery code to show and hide divs on my page. As
I'm using this code for my web assignment: http://csstechniques.blogspot.sg/2007/05/super-simple-css-bars.html But how do I show
I'm using this code: <script> $(document).ready(function () { $.getJSON(http://twitter.com/statuses/user_timeline/USERNAME.json?callback=?, function(data) { $(.show_tweet).html(data[0].text); }); });
So I'm using this code: <iframe src=http://www.connect.facebook.com/widgets/fan.php?href=http://www.facebook.com/pages/TopDesenecom/148037301911330&width=205&height=256&show_faces=true&stream=false&header=false&css=http://topdesene.com/template/fb.css></iframe> and facebook shows this code: <link href=http://external.ak.fbcdn.net/fbml_static_get.php?src=http%3A%2F%2Ftopdesene.com%2Ftemplate%2Ffb.css&appid=148037301911330&pv=1&sig=59bf9a037df3a88cb6ff142f25227177&filetype=css&cb=2

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.