0

I'm writing a graphics editor. Some components is displayed on a pane of editor. They must react on a mouse clicks. At the same time the pane of editor must react on a clicks too. For example, we have two buttons on a pane. We click on a first button and the button will change its displaying (will look as launched). Then we click on a second button and it will do the same. And at the same time the pane of editor will draw line from first button to second button.

Here is my test code. I set my pane of editor as a glass pane and added a listener to it. I added my buttons to a content pane and added a listener to a top button. When I click on the top button, i will see that the pane's listener process, but the button's listener will not process and not change its displaying!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame {
    public Test() {
        super("Test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 200);

        JButton bottomBtn = new JButton("On bottom");
        bottomBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("JButton.mouseClicked()");
            }
        });
        JButton topBtn = new JButton("On top");

        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.add(bottomBtn, "South");
        contentPane.add(topBtn, "North");
        setContentPane(contentPane);

        ControlPanel control = new ControlPanel();
        setGlassPane(control);

        setVisible(true);
        control.setVisible(true);  // is there any other way?
    }

    public static void main(String[] args) {
        new Test();
    }

    private class ControlPanel extends JComponent implements MouseListener {
        public ControlPanel() {
            setOpaque(false);
            addMouseListener(this);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println("ControlPanel.mouseClicked()");
        }

        @Override
        public void mouseEntered(MouseEvent e) {}
        @Override
        public void mouseExited(MouseEvent e) {}
        @Override
        public void mousePressed(MouseEvent e) {}
        @Override
        public void mouseReleased(MouseEvent e) {}
    }
}
pto3
  • 513
  • 1
  • 5
  • 13
  • Yep, the ControlPanel is consume mouse events, this is typically how you "block" events from getting to the rest of the app. Why does the ControlPanel need to monitor mouse events? – MadProgrammer Sep 04 '15 at 09:19
  • 1
    Depending on what you are trying to do, it might be better to use a global event listener, see [`Toolkit#addAWTEventListener`](https://docs.oracle.com/javase/8/docs/api/java/awt/Toolkit.html#addAWTEventListener-java.awt.event.AWTEventListener-long-) for more details – MadProgrammer Sep 04 '15 at 12:55
  • I allocate all acts on atwo groups. All acts with single component (move component, double click, right click ets.) will process into there components. The other acts with two or more components (click on first then click on second component, ets.) will process in some object, called by me ControlPanel. I think that the ControlPanel may be above all components. The ControlPanel is not necessary set into a GlassPane. Where is better to place it? – pto3 Sep 04 '15 at 13:07
  • `contentPane.add(bottomBtn, "South");` - don't use magic strings. How do you know "South" is a valid value that is not defined in the API anywhere. However, `BorderLayout.SOUTH` is defined in the API. Use the field names from the API. – camickr Sep 04 '15 at 14:29
  • Well, I read a book writing by Ivan Portyankin (I think, he is one of the authors of a swing library) and he uses that magic strings. He said it is two ways to place components into a BorderLayout: 1) BorderLayout.SOUTH, 2) "South" ("East", "West" ets.). Finally, the second way works as well as the first way :) Thanks for you comment, you are right. – pto3 Sep 05 '15 at 18:58
  • MadProgrammer, thanks for the way to seeking the answer! – pto3 Sep 06 '15 at 19:17
  • Here is the answer on my question: http://stackoverflow.com/a/32427249/5149064 – pto3 Sep 06 '15 at 19:18

0 Answers0