• Post Reply Bookmark Topic Watch Topic
  • New Topic

Selecting JPanels

 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a "market" kinda like what you might find in a video game, e.g you can buy swords, potions, etc.
I'm laying it out as follows. There is a JInternalFrame which contains a JTabbedPane. The JTabbedPane contains a different JPanel for each category; obviously only one JPanel/category can be seen at a time. Within each JPanel is a JScrollPane containing a scrollable JPanel. That inner JPanel is using a VerticleFlowLayout to display ProductPanels (which simply extend JPanels).
In short, you can flip between scrollable lists of JPanels. I want to let the user click on one of my ProductPanels (efectively JPanels) and be able to figure out which JPanel was clicked on. I'm not quite sure how to do this. Any ideas?
--Mark
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call getParent() on any Component to get a reference to the Container it's in, so code can call getParent() on a ProductPanel and simply chain the calls upward until they hit one of those category panels. If we propose a CategoryPanel class, then all you have to do is
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, I don't follow.
I already know who the panel's parent is. I want to know, when the user clicks on a panel, which panel it clicked on. Is there some way to capture the mouse click and figure out in what panel it was (without using glass panes)? I also don't want to place a button in the panel and require the user to click on that, I'd rather that they can click anywhere on the panel.
--Mark
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't seem to be doing very well in understanding people's questions today. I thought you wanted to know which category panel the clicked ProductPanel was inside of.
Just add a mouse listener to ProductPanel; when you click on a ProductPanel, that method will be invoked, no matter how many containers it's nested inside of. It will be invoked on the actual ProductPanel object that was clicked; now if you need to figure out the category, you can add the code I showed previously into the event handler.
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, a mouse listener! Duh!
Thanks. :-p

--Mark
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!