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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:34:50+00:00 2026-06-19T02:34:50+00:00

So I have the following class. I have over-simplified the code, as putting 500+

  • 0

So I have the following class. I have over-simplified the code, as putting 500+ lines of code is not an option. It is basically a very fancy square:

public class ModuleGui extends JComponent implements ElementInterface {

private String name;

private Rectangle2D s = new Rectangle2D.Double();
private Rectangle2D[] points;
private int resizeSize = 10;

private final ShapeResizeHandler shapeResizeHandler = new ShapeResizeHandler();

public ModuleGui(int x, int y){

    this.addMouseListener(shapeResizeHandler);
    this.addMouseMotionListener(shapeResizeHandler);

    this.x = x;
    this.x = y;

    points = new Rectangle2D[2];
    points[0] = new Rectangle2D.Double(x,y,1,1);
    points[1] = new Rectangle2D.Double(x + width, y + height, resizeSize, resizeSize);

    this.name = new String("Gate" + Integer.toString(namingCounter++));
}

public void paintComponent(Graphics g){
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.fill(points[1]);

    //Set the main rectangle, fill it and draw it
    s.setRect(points[0].getX(), points[0].getY(),
            Math.abs(points[1].getCenterX() - points[0].getCenterX()),
            Math.abs(points[1].getCenterY() - points[0].getCenterY()));
    g2d.setColor(Color.WHITE);
    g2d.fill(s);
    g2d.setColor(Color.BLACK);
    g2d.draw(s);
    //Main rectangle draw end

    //Add the name and the & symbol
    g2d.drawString(this.name, (int) (s.getCenterX()), (int) (s.getY() + this.height/10));
}


private class ShapeResizeHandler extends MouseAdapter{
    public void mousePressed(MouseEvent e){
        System.out.println("Funny");
    }

    public void mouseReleased(){
        //Do more stuff
    }

    public void mouseDragged(MouseEvent e){
        //Do Stuff
        repaint();
    }
}
    }

Now I have this JComponent inside JPanel..but I don’t seem to catch any mouse events. The mouse events show up in the JPanel, but not in the JComponent. I have tried to make a simple mouse listener to just print something, but the same happens.

  • 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-19T02:34:51+00:00Added an answer on June 19, 2026 at 2:34 am

    Make sure your component is visible and has non-zero dimensions. Here is code that works for me:

    public class MouseTest extends JComponent {
        public MouseTest () {
            addMouseListener (new MouseAdapter () {
                @Override
                public void mousePressed (MouseEvent e) {
                    System.out.println ("Mouse pressed");
                    e.consume();
                }
            });
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension (320, 240);
        }
    
        @Override
        public void paint(Graphics g) {
            g.setColor (Color.cyan);
            g.fillRect (getX (), getY (), getWidth (), getHeight ());
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame ("Mouse Test");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane ().setLayout (new BorderLayout ());
            frame.getContentPane ().add (new MouseTest (), BorderLayout.CENTER);
            frame.pack ();
            frame.setVisible (true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which animates the image class while hovering over the
I have following code class User attr_accessor :name end u = User.new u.name =
I have the following setup (simplified, obviously): An abstract class, with an A object
I have the following problem: I want to send a type (java.lang.Class) over the
So, I have the following object (simplified for sake of example): public class SomeListener
I have the following code: class CSplit(var s1: CanvNode, var s2: CanvNode) extends SplitPane
I have created the following class that extends JSpinner to iterate over dd/mm/yyy values.
I have the following simplified HTML Below. <div id=coat> <span class=Jan2011-Sales>10</span> <span class=Feb2011-Sales>10</span> <span
I have the following situation (simplified): a.h: #include <boost/serialisation/serialisation.hpp> class B; using namespace std;
I have the following class which I'm fighting over how to implement. The question

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.