Results 1 to 14 of 14
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default mouse listing over JPanels

    Is this going to work?
    I created a 100 jpanels covered with jlabels. and registered a mouselistener,
    I'd like to read out on which jpanel I'd double clicked....
    Is this going to work?
    I might need a glasspane?
    Any suggestions welcome:



    Java Code:
     public JPanel CreatePanels() {
    
            int arraycount= 0;          // panel CENTER
            roomPanel = new JPanel();
            roomPanel.setLayout(new GridLayout(10,10));
            JPanel rp;
            for (int i = 3; i <6; i++) {
                for (int ii = 0; ii <=29; ii++) {
                    int room = (i*100)+ii;
                    rp = singleroompanel.CreateSingleRoomPanel(new JPanel(), room);
    
                    roominfopanel[arraycount].setRoomNumber(room);
                    roominfopanel[arraycount].setPanelRef(rp);
    
                    if (roominfopanel[arraycount].getPianoRoom());
                    arraycount++;
                    rp.addMouseListener(Mhandler);
                    roomPanel.add(rp);
                }
            }
            // set panel text
            return roomPanel;
         }

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    This is not working. The class which contains the method extends JFrame.
    When registering the listener with the frame I get mouse readings alright,
    but than I can not see on which of the 100 panels I am clicking....

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default Re: mouse listing over JPanels

    One way: create a class that extends MouseAdapter and has a (probably final) int field to represent 'room' that is initialized from a constructor parameter. Construct and add this listener to each JPanel.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    db here is what I did:
    1) I set up a inner class that implements the MouseListener,
    planing on using the mouseEntered method:


    Java Code:
    public class MouseHandler implements MouseListener {
            public void mousePressed(MouseEvent e) {
                    System.out.println(" mouse pressed at " + e);
                     //   e.getClickCount()
            }
    
            public void mouseReleased(MouseEvent e) {
    
            }
    
            public void mouseEntered(MouseEvent e) {
               System.out.println(" mouse entered at " + e);
               System.out.println(e.getSource().getClass());
            }
    
            public void mouseExited(MouseEvent e) {
    
            }
    
            public void mouseClicked(MouseEvent e) {
    
            }
    
    
       }
    2) than I'll register each room panel (JPanel) with the created listener object
    called Mhandler:

    Java Code:
    public JPanel CreatePanels() {
    
            int arraycount= 0;          // panel CENTER
            roomPanel = new JPanel();
            roomPanel.setLayout(new GridLayout(10,10));
            
            for (int i = 3; i <6; i++) {
                for (int ii = 0; ii <=29; ii++) {
                    int room = (i*100)+ii;
                    
                    rp = singleroompanel.CreateSingleRoomPanel(new JPanel(), room);
    
                    roominfopanel[arraycount].setRoomNumber(room);
                    roominfopanel[arraycount].setPanelRef(rp);
    
                    if (roominfopanel[arraycount].getPianoRoom());
                    
                    rp.addMouseListener(Mhandler);
                    roomPanel.add(rp);
                    arraycount++;
                }
            }
            
            return roomPanel;
         }
    3) But the point is I do not get any mousereadings, maybe the problems lays
    in the setup of the panels, or maybe the panels are to populated and
    covering the sureface?

    Java Code:
    class DeskMonitor extends JFrame{
        private JPanel panel, roomPanel, infopanel, rp;;
        private JLabel label, infolabel;
        private final static int MAXROOM = 90;
        private RoomInfoPanel[] roominfopanel;
        private SingleRoomPanel singleroompanel;
        private ImageIcon icon1, icon2;
        private MouseHandler Mhandler;
    
        public DeskMonitor()  {
          
          roominfopanel = new RoomInfoPanel[MAXROOM]; // initialize the
          for (int i = 0; i < roominfopanel.length; i++) { // roominfopanrl array
              roominfopanel[i] = new RoomInfoPanel();
          }
    
          icon1 = createImageIcon("images/keyboard.gif");
          icon2 = createImageIcon("images/lic2.gif");
    
          setTitle("Principle Desk Monitor");
          setSize(1600, 900);
          panel = new JPanel(new BorderLayout());
          // object which constructs ind room panels
          singleroompanel = new SingleRoomPanel();
    
    
          panel.add(CreatePanels(), BorderLayout.CENTER);
          panel.add(createTopInfoPanel(), BorderLayout.NORTH);
    
          Mhandler = new MouseHandler();
          
          Container contentPane = getContentPane();
          contentPane.add(panel);
         // myGlassPane.setVisible(true);
        }

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    I'd first like to understand what is going on:
    1) the class extends JFrame
    2) the principle panel that covers the JFrame is called "panel'
    and is added to the contentPane of the Container (Parent of JFrame)
    3) The 100 room panels that register the mouse listener are added to "panel"
    So I believe this should work, right?...... wrong it does not!

    (actually "roompanel" (whixh has a gridlayout of 10 by 10 holds the room panels
    and is added to panel (borderlayout center))
    Last edited by willemjav; 03-20-2013 at 04:38 PM.

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    The roompanel is totally covered with many JLabels!
    Maybe that causes the mouse not the fire?
    I could call the glassPane of each room panel,right?
    But probably I missing some other "tiny detail" again?

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    So when I say panel.addMouseListener(Mhandler); in the constructor
    I get readings!

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    But When I say roomPanel.addMouseListener(Mhandler); in the CreatePanels()
    I get ZIP, wow why is that, I do not get this!


    Java Code:
     public JPanel CreatePanels() {
    
            int arraycount= 0;          // panel CENTER
            roomPanel = new JPanel();
            roomPanel.setLayout(new GridLayout(10,10));
            roomPanel.addMouseListener(Mhandler);

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    And I am not even yet to the level of the 100 small JPanels I'd like to register!

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default Re: mouse listing over JPanels

    That's what I recommended at #3. I also recommended that you extend MouseAdapter which will allow you to reduce the clutter of empty methods.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    Ok I found the problem I created the mousehandler to late in the constructor
    It should be created before adding it, again a stupid mistake.....
    thread closed!

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    (do please not losse faith in my db)

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    Wait db, I am still here, what could one do to see which JPanel of 100 is entered by the mouse
    (I do have the ref of each panel in a array)

    public void mouseEntered(MouseEvent e) {
    System.out.println(" mouse entered at " + e);
    System.out.println(e.getSource().getClass());
    }

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: mouse listing over JPanels

    Found it db:
    if (e.getSource() == roominfopanel[3].getPanelRef())
    System.out.println(" bingo ");

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 02:50 PM
  2. listing files at the FTP
    By oulutas in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 11:30 AM
  3. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  4. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM
  5. Listing all available Locales
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 04:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •