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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Daniel
  • 179
  • 2
  • 9
  • 1
    Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. – trashgod Feb 20 '13 at 12:41
  • Now that I think about it, maybe it has something to do with the fact that I have not set bounds and have not practically defined the space in which the component lies. – Daniel Feb 20 '13 at 12:42
  • 2
    Use a layout and override [`getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Feb 20 '13 at 12:43

1 Answers1

1

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);
    }
}
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
  • Then I must be doing something wrong around here. Will have to take a closer look. – Daniel Feb 20 '13 at 13:32
  • I have other problems regarding the JComponent showing correctly, but your example works. Thank you :) – Daniel Feb 20 '13 at 14:17