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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:56:59+00:00 2026-06-18T04:56:59+00:00

What I am trying to do: draw a vertical line and a horizontal line

  • 0

What I am trying to do: draw a vertical line and a horizontal line that are perpendicular to each other and meet where the mouse points. A sort of cursor tracker.

My structure: JFrame -> CustomPanel -> other panels/components etc.

CustomPanel inherits from JPanel and it’s set as my JFrame’s ContentPane.

I tried to use a GlassPane, everything worked perfectly, but I want to keep my events, not disable them. I still want to be able to click buttons etc.

A relevant question is this Painting over the top of components in Swing?. Everything behaves as expected when I move my mouse where there are empty places in my CustomPanel, but it still doesn’t paint over the other components.

In the image it should have continued painting over the button, but it stopped when I entered it and then resumed when I exited.

enter image description here

Code below.

public class Painter {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame();

        frame.setSize(600, 600);
        frame.setPreferredSize(new Dimension(600, 600));
        frame.setContentPane(new CustomPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class CustomPanel extends JPanel {
    int x = 0;
    int y = 0;

    public CustomPanel() {

        addMouseListener(new AdapterImplementation(this));
        addMouseMotionListener(new AdapterImplementation(this));
        add(new JButton("TESTBTN"));
        setSize(new Dimension(600, 600));
        setPreferredSize(new Dimension(600, 600));
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawLine(0, y, getWidth(), y);
        g.drawLine(x, 0, x, getHeight());
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }
}

and my adapter:

public class AdapterImplementation extends MouseAdapter {
    CustomPanel pane;

    public AdapterImplementation(CustomPanel pane) {
        this.pane = pane;
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        pane.setX(x);
        pane.setY(y);
        pane.repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        System.out.println("MOUSE MOVED");
        int x = e.getX();
        int y = e.getY();
        pane.setX(x);
        pane.setY(y);
        pane.repaint();
    }
}
  • 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-18T04:57:00+00:00Added an answer on June 18, 2026 at 4:57 am

    The problem here is that the MouseListeners are registered with your CustomPanel but not with the JButton so the latter does not process events from the listeners.

    Also, as you’ve seen, events to underlying components will be blocked when using GlassPane.

    A JLayeredPane could be used to as a topmost container to capture the MouseEvents using your current listeners.

    Note: Override paintComponent instead of paint for custom painting in Swing and remember to invoke super.paintComponent(g).

    • JLayeredPane Example
    • How to Use Layered Panes
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to draw a line along with mouse move on a paper.
I'm trying to build a ScrolledWindow that you can draw on using the mouse,
I am trying to draw horizontal and vertical lines on a bufferedimage. It should
I'm trying to draw vertical text in win32 GDI api. I call CreateFont() with
I'm trying to draw a vertical scrollbar for my G15 applet, but am having
I am trying to draw over a canvas by clicking and dragging the mouse.
I'm trying to draw a line with soft edges, regardless of the slope. Here's
I'm trying to draw a 2D image on the screen that is always facing
I have an ARC project and am trying to draw a vertical linear gradient.
I'm learning Postscript I'm trying to create a method for that would draw 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.