Dejwi
Dejwi

Reputation: 4497

Border of JPanel with mouseEvent handling

How to create a border of JPanel, which will be able to handle MouseEvents?

I tried to do something like that:

abstract public class MyBorder extends LineBorder implements MouseListener

But after implementing virtual methods I cannot assign mouseListener to my class. I guess, that I have to assign it into some JComponent.

So, how can I create some sort of border with mouseListener?

Upvotes: 2

Views: 2869

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168845

Here is an SSCCE that supports that borders get mouse events on the component to which they are applied.

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

class BorderListener {

    private void initGui() {
        final JPanel gui = new JPanel();
        gui.setBackground(Color.green);
        gui.setPreferredSize(new Dimension(300,50));
        gui.setBorder(new LineBorder(Color.blue, 10));
        gui.addMouseListener( new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                System.out.println(me.getPoint());
            }
        });
        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                BorderListener bl = new BorderListener();
                bl.initGui();
            }
        });
    }
}

Typical Output

When clicking in the wide border assigned to this panel, you might see output along these lines.

java.awt.Point[x=8,y=3]
java.awt.Point[x=3,y=26]
java.awt.Point[x=1,y=43]
java.awt.Point[x=15,y=6]
java.awt.Point[x=101,y=5]
java.awt.Point[x=220,y=4]
java.awt.Point[x=287,y=5]
java.awt.Point[x=295,y=3]
Press any key to continue . . .

The border is 10px wide, so if (x||y < 10), it is within the line border.


Update

(Comment to camickr, which also applied to my answer)

Yes but then this mouseListener will be added for the whole JPanel. Not only for my Border. Am I wrong?

Just ignore the event if it happens in the non-border area of the panel.

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

class BorderListener {

    private void initGui() {
        final JPanel gui = new JPanel();
        gui.setBackground(Color.yellow);
        gui.setPreferredSize(new Dimension(300,50));
        gui.setBorder(new LineBorder(Color.orange, 15));
        gui.addMouseListener( new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                int w = gui.getWidth();
                int h = gui.getHeight();
                int x = me.getPoint().x;
                int y = me.getPoint().y;
                Insets ins = gui.getInsets();
                boolean inBorder =
                    ( x<ins.left ||
                    x>w-ins.right ||
                    y<ins.top ||
                    y>h-ins.bottom);
                if (inBorder) {
                    System.out.println(me.getPoint());
                } else {
                    System.out.println("Ignore!");
                }
            }
        });
        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                BorderListener bl = new BorderListener();
                bl.initGui();
            }
        });
    }
}

Output

java.awt.Point[x=168,y=7]
Ignore!
java.awt.Point[x=164,y=41]
java.awt.Point[x=297,y=39]
java.awt.Point[x=297,y=21]
Ignore!
Ignore!
java.awt.Point[x=2,y=21]
Press any key to continue . . .

Upvotes: 2

camickr
camickr

Reputation: 324207

A MouseListener must be added to a Component, not a Border. So to use your class the code would need to be something like:

Border border = new MyBorder();
panel.setBorder( border );
panel.addMouseListener( border );

Upvotes: 3

Related Questions