Results 1 to 14 of 14
Thread: mouse listing over JPanels
- 03-19-2013, 10:40 PM #1
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
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; }
- 03-19-2013, 11:29 PM #2
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
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....
- 03-20-2013, 03:53 AM #3
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-20-2013, 04:29 PM #4
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
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:
2) than I'll register each room panel (JPanel) with the created listener objectJava 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) { } }
called Mhandler:
3) But the point is I do not get any mousereadings, maybe the problems laysJava 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; }
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); }
- 03-20-2013, 04:35 PM #5
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
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.
- 03-20-2013, 04:42 PM #6
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
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?
- 03-20-2013, 04:49 PM #7
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
Re: mouse listing over JPanels
So when I say panel.addMouseListener(Mhandler); in the constructor
I get readings!
- 03-20-2013, 04:52 PM #8
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
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);
- 03-20-2013, 04:54 PM #9
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
Re: mouse listing over JPanels
And I am not even yet to the level of the 100 small JPanels I'd like to register!
- 03-20-2013, 04:55 PM #10
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-20-2013, 05:07 PM #11
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
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!
- 03-20-2013, 05:08 PM #12
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
Re: mouse listing over JPanels
(do please not losse faith in my db)
- 03-20-2013, 05:11 PM #13
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
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());
}
- 03-20-2013, 05:34 PM #14
Senior Member
- Join Date
- Dec 2007
- Location
- Spain
- Posts
- 637
- Rep Power
- 6
Similar Threads
-
Switching JPanels with a button inside one of the JPanels
By Beastly in forum AWT / SwingReplies: 2Last Post: 04-26-2011, 02:50 PM -
listing files at the FTP
By oulutas in forum New To JavaReplies: 6Last Post: 10-10-2010, 11:30 AM -
Problems regarding JPanels in JPanels
By ColtonPhillips in forum AWT / SwingReplies: 2Last Post: 07-19-2010, 08:33 PM -
Mouse Listener for mouse floating over object?
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 04:34 AM -
Listing all available Locales
By Java Tip in forum Java TipReplies: 0Last Post: 12-29-2007, 04:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks