JLabel - get parent panel?

后端 未结 3 1241
情深已故
情深已故 2020-12-18 00:32

Is it possible to get the name of the panel that a JLabel is added to? I added some JLabels to different JPanels, and I want to call different mouse events according to whic

3条回答
  •  天命终不由人
    2020-12-18 01:02

    If you don't know how deep down your label is in the hierarchy, there's some handy functions in SwingUtilities, for example: SwingUtilities.getAncestorOfClass(Class, Component)

    An alternative to checking the name of the parent container in your labels could be to simply forward the event to the parent container instead and let the parent do the work. Depending on what you're trying to achieve, it could be useful to decouple the labels from the parents. Forwarding events can be done using Component.processEvent(AWTEvent).

提交回复
热议问题