Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: 2 MouseListeners at the same location in a layeredPane

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post 2 MouseListeners at the same location in a layeredPane

    I have a JLayeredPane 'lp' which contains a JPanel 'p' with a JLabel 'l' on it.

    If the label is clicked then I print out the message 'label1 was clicked' and then add another JPanel 'p2' to the JLayeredPane 'lp' on top of 'p', with another JLabel on 'p2' say 'l2'.

    If the label 'l2' is clicked it should print out 'label2 was clicked'.

    The Labels l1 & l2 overlap each other on the LayeredPane. On running, when l is clicked, it displays 'p2' with 'l2' but after that if 'l2' is clicked, it does not say 'label2 was clicked'. Instead it just keeps saying 'label1 was clicked' and keeps adding more and more panels.

    Here is an MCVE which demonstrates the problem..

    1. Main class

    package com.company;
     
    public class Main {
     
        public static void main(String[] args) {
            Frame f = new Frame();
            f.setVisible(true);
        }
    }

    2. Frame class

    package com.company;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
    public class Frame extends JFrame {
        MouseAdapter listener;
     
        public Frame()
        {
            super.setSize(600,600);
            super.setLayout(new BorderLayout());
     
            JLayeredPane layeredPane = new JLayeredPane();
            layeredPane.setSize(600, 600);
     
            listener = new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    System.out.println("Label1 was clicked");
     
                    JPanel panel2 = new JPanel();
                    panel2.setBackground(Color.RED);
                    panel2.setBounds(270,0,60,60);
     
                    JLabel label2 = new JLabel("LABEL 2");
                    label2.setBounds(0,0,60,60);
                    panel2.add(label2);
     
                    layeredPane.add(panel2, 2);
                    label2.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            System.out.println("Label 2 was clicked.");
                        }
                    });
                }
            };
     
            JPanel panel1 = new Panel(layeredPane);
     
            JLabel label1 = new JLabel("LABEL 1");
            label1.setBounds(0,0,60,60);
            panel1.add(label1);
            label1.addMouseListener(listener);
     
            layeredPane.add(panel1, 1);
     
            super.add(layeredPane);
            super.validate();
        }
    }

    3. Panel class

    package com.company;
     
    import javax.swing.*;
    import java.awt.*;
     
    public class Panel extends JPanel {
        JLayeredPane layeredPane;
     
        public Panel(JLayeredPane layeredPane) {
            this.layeredPane = layeredPane;
     
            super.setSize(600,600);
            super.setBackground(Color.BLUE);
     
        }
    }

    If this code is compiled, the problem can be reproduced.

    Thanks for taking the time to help me out.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: 2 MouseListeners at the same location in a layeredPane

    It appears that you didn't set the correct layer for the for the new layered Panel. I have included some code changes as well as suggestions. I am not an expert on JLayeredPane's so I can't really help much with that without do some testing of my own.

    Regards,
    Jim

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    import javax.swing.*;
     
    public class Main {
     
       public static void main(String[] args) {
          // always a good idea
    	  SwingUtilities.invokeLater(() -> new Frame());
       }
    }
    //public class Frame extends JFrame {
    // extending any class (especially JFrame) is not good practice unless you are
    // overriding something. Use composition over inheritance when possible
    class Frame {
       JFrame  frame = new JFrame(); // created the frame
       MouseAdapter	listener;
     
       public Frame() {
    	  // super.setSize(600, 600); probably ignored by layout manager
    	  frame.setPreferredSize(new Dimension(600, 600));
     
    	  // added the following so when you close the frame the app exits
    	  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	  frame.setLayout(new BorderLayout());
     
    	  JLayeredPane layeredPane = new JLayeredPane();
    	  layeredPane.setSize(600, 600);
     
    	  listener = new MouseAdapter() {
    		 @Override
    		 public void mouseClicked(MouseEvent e) {
    			System.out.println("Label1 was clicked");
    			JLabel label2 = new JLabel("LABEL 2");
    			JPanel panel2 = new JPanel();
    			panel2.setBackground(Color.RED);
    			panel2.setBounds(270, 0, 60, 60);
    			label2.setBounds(0, 0, 60, 60);
    			panel2.add(label2);
     
    			layeredPane.add(panel2, 0); // <-- changed from 2 to 0
    			label2.addMouseListener(new MouseAdapter() {
    			   @Override
    			   public void mouseClicked(MouseEvent e) {
    				  System.out.println("Label 2 was clicked.");
    			   }
    			});
     
    		 }
    	  };
     
    	  JPanel panel1 = new Panel(layeredPane);
     
    	  JLabel label1 = new JLabel("LABEL 1");
    	  label1.setBounds(0, 0, 60, 60);
    	  panel1.add(label1);
    	  label1.addMouseListener(listener);
    	  layeredPane.add(panel1, 1);
     
    	  frame.add(layeredPane);
    	  // super.validate();
    	  frame.pack(); // read API for description
    	  frame.setLocationRelativeTo(null); // centers on screen - check API
    	  frame.setVisible(true); // removed from Main
       }
    } 
     
    class Panel extends JPanel {
       JLayeredPane layeredPane;
     
       // Instead of extending JPanel which serves no purpose
       // why not make a method in Frame.java that simply creates a
       // layeredPane, and adds a JLabel, text, and mouse listener
       // and then returns the layeredPane
     
       public Panel(JLayeredPane layeredPane) {
    	  this.layeredPane = layeredPane;
     
    	  // super.setSize(600, 600); not needed
    	  // super.setBounds(0, 0, 60, 60);
    	  setBounds(270, 0, 60, 60);// methods are inherited - super not required
    	  super.setBackground(Color.BLUE);
     
       }
    }

Similar Threads

  1. Specifying the file location
    By fahman_khan75@yahoo.com in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 12th, 2014, 08:36 AM
  2. exchange 2 JLabels Location
    By haiderali in forum Java Theory & Questions
    Replies: 5
    Last Post: May 7th, 2013, 02:01 PM
  3. first empty location in an array!?
    By keep smiling in forum Collections and Generics
    Replies: 2
    Last Post: February 10th, 2012, 06:29 AM
  4. Returning location instead of value?
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2011, 04:39 PM
  5. [SOLVED] How to specify FileOutputStream location?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 29th, 2011, 06:36 PM

Tags for this Thread