Skip to main content
gamedev.net
🔒 Locked

[java] Swing JTabbedPane MouseListeners

Started by Vyper_uk Aug 5, 2005 at 2:31 PM 1 replies 450+ views
Original Post
Vyper_uk
Vyper_uk
I'm making a simple editor for tile based maps, and I'm using a JTabbedPane with canvas classes in each tab to represent the different layers of my map (eg the bottom layer of tiles only shows if there is nothing on the layers above it etc). What I want to do is add appropriate mouselisteners to the tabbedpane so that I can paint tiles onto the canvases using the mouse. However, mouselisteners added to the JTabbedPane class only seem to work on the selection tabs, and not the things actually in the tabs (my canvases). So from what I can see I have two options. The first is to make a seperate mouse listener for each canvas and bind them to the actual canvases. Would this have a big performance hit (I'm looking at about 4 tabs)? The second is to have one mouselistener and add a ChangeListener to the tabbedpane which removes the mouse listener from the tab which is no longer visible, and then adds it to the one which has become visible. This all seems very messy, is there a better way of doing it? And if not which of these methods is the 'cleanest' and best for performance? Thanks
pinacolada
pinacolada
It's not a performance hit to have a bunch of MouseListeners sitting around listening to different things. The great thing about the Listener pattern is that it doesn't actually do anything until its used. Say you have panels A,B,C. A gets a click, the system finds A's listeners, A's listener receives the click. Whether or not B and C have listeners (maybe even the *same* listener) doesn't affect this process at all.

And in the interests of code cleanliness, you don't need to make the MouseListeners seperate objects. Any object can be a MouseListener. And you can use one object to listen to multiple things (as long as your logic won't get confused by this).
Vyper_uk
Vyper_uk
Ok thanks a lot :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.