I have a JFrame
. When I click in the frame, I want to add a Component
(in this case an extension of a Canvas
). I added a MouseListener
to the frame as follows:
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
frame.add(canvas);
frame.repaint();
}
});
However, the component is not added when the mouse is clicked on the frame. I have no problem adding the component in my main method. Adding a print statement in the mouse listener prints correctly, as does removing components from the frame.