javaactionlisteneronmouseclick

Implement of listener does not work


public class ShapeDisplayer implements ActionListener {
    private static final int Width = 50; 
    private static final int Frame_Width = 400;
    private static final int Frame_Height = 600; 
    static JPanel DrawPanel;
    static JButton carButton;
    static JButton eclipseButton;
    static JButton compButton;

    public static void main (String args[])
    {
        // create frame
        JFrame frame = new JFrame("Drawing Applet"); 

        // create panels
        JPanel ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        DrawPanel = new JPanel(new BorderLayout());

        // create Icons
        CarIcon Acar = new CarIcon(Width);
        EclipseIcon doubleCircle = new EclipseIcon(20);
        CompositeIcon compIcon = new CompositeIcon(50, 60);

        // create buttons
        carButton = new JButton(Acar);
        eclipseButton = new JButton(doubleCircle);
        compButton = new JButton(compIcon);

        // panel config
        ButtonPanel.setBounds(0,0,Frame_Width,100);
        DrawPanel.setBounds(0,100,Frame_Width,Frame_Height-100);

        // add panels'components
        ButtonPanel.add(eclipseButton);
        ButtonPanel.add(carButton);
        ButtonPanel.add(compButton);

        // add frames' components
        frame.add(ButtonPanel);
        frame.add(DrawPanel);

        // frame config
        frame.setSize(Frame_Width, Frame_Height);
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == carButton)
        {
            DrawPanel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) { 
                  CarIcon car = new CarIcon(Width, e.getX(), e.getY());
                  JLabel lable = new JLabel (car);
                  DrawPanel.add(lable);
              } 
            });     
        }

    }
}

Im trying to implements this program to draw on the panel on mouseclick, for example if the button is carButton, then whenever i click on the panel, a car icon will be drew. I implemented the listener, but when I clicks on the panel nothing appears on the panel.I'm adding the listener on the Jframe, do I need to add it on the Jpanel for the program to work properly? Can someone help to fix this program, thank in advance.

enter image description here


Solution

  • The actionPerformed method does not get invoked when a button is clicked. Because it is not bound to any of the buttons.

    For instance, the carButton can be added an action listener as follows.

    carButton = new JButton(Acar);
    carButton.addActionListener(new ShapeDisplayer());
    

    Note that as this code is in the main method, "this" cannot be used to add an action listener. A new object of ShapeDisplayer class needs to be instantiated.

    Also the DrawPanel may need a repaint for the changes to take effect. Invoking revalidate method does that.

     DrawPanel.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) { 
            CarIcon car = new CarIcon(Width, e.getX(), e.getY());
            JLabel lable = new JLabel (car);
            DrawPanel.add(lable);
            DrawPanel.revalidate();
        } 
    });