Search code examples
javaswingmouseeventjcomponent

JComponent not registering


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.


Solution

  • 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);
        }
    }