How to get the parent object of a clicked component using getParent()?

839 views Asked by At

So this is a snippet of a larger program. Not shown in the below code is a JFrame "framer" which I have properly set up and which also contains a JPanel with various JLabels. What I am aim for in the below code is a way for me to access the object "face" which is of type "facer" which extends JPanel, where "facer" is properly added to the JFrame "framer" with all of this displaying properly. The below code shows some things that I have tried. Regardless of where I use .getName() I always get null. "line4" gets me access to scrollPane rather than the object, which I also have added to the JFrame. I also find it odd that getParent().getParent() gives a JLayeredPane (which I am not using) rather than a JFrame.

All of this is basically a watered down version of the real area of code I'm stuck on but solving this would essentially show me what to do in the other scenario. Basically, all I'm asking is how I can get getParent().getName() or getParent().getParent().getName() to print out the object's name (which in this case is "facer" or "framer") rather than null or its type information as shown in my outputs below.

face.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {
                Component source = (Component) e.getSource(); //line1
                System.out.println("source gives me " + source); //line2
                System.out.println("getName() gives me " + source.getName()); //line3
                System.out.println("getParent().getComponent(1) gives me " + source.getParent().getComponent(0)); //line4
                System.out.println("getParent().getName() gives me " + source.getParent().getName()); //line5
                System.out.println("getParent().getParent() gives me " + source.getParent().getParent()); //line6
                System.out.println("getParent().getParent() gives me " + source.getParent().getParent().getName()); //line7

            }
        });

The output gives me

    source gives me facer[,0,0,200x288,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
    getName() gives me null
    getParent().getComponent(1) gives me javax.swing.JScrollPane[,30,-18,140x18,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.metal.MetalBorders$ScrollPaneBorder@55e9e368,flags=328,maximumSize=,minimumSize=,preferredSize=,columnHeader=,horizontalScrollBar=javax.swing.JScrollPane$ScrollBar[,0,0,0x0,hidden,layout=javax.swing.plaf.metal.MetalScrollBarUI,alignmentX=0.0,alignmentY=0.0,border=,flags=4194632,maximumSize=,minimumSize=,preferredSize=,blockIncrement=10,orientation=HORIZONTAL,unitIncrement=1],horizontalScrollBarPolicy=HORIZONTAL_SCROLLBAR_NEVER,lowerLeft=,lowerRight=,rowHeader=,upperLeft=,upperRight=,verticalScrollBar=javax.swing.JScrollPane$ScrollBar[,0,0,0x0,hidden,layout=javax.swing.plaf.metal.MetalScrollBarUI,alignmentX=0.0,alignmentY=0.0,border=,flags=4194632,maximumSize=,minimumSize=,preferredSize=,blockIncrement=10,orientation=VERTICAL,unitIncrement=1],verticalScrollBarPolicy=VERTICAL_SCROLLBAR_NEVER,viewport=javax.swing.JViewport[,1,1,137x15,layout=javax.swing.ViewportLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=25165832,maximumSize=,minimumSize=,preferredSize=,isViewSizeSet=true,lastPaintPosition=,scrollUnderway=false],viewportBorder=]
    getParent().getName() gives me null.contentPane
    getParent().getParent() gives me javax.swing.JLayeredPane[null.layeredPane,0,0,200x288,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,preferredSize=,optimizedDrawingPossible=true]
    getParent().getParent() gives me null.layeredPane
1

There are 1 answers

0
rolfl On

You say: 'object "face" which is of type "facer" which extends JPanel'. I read this as saying you have something like:

public class facer extends JPanel { ... }

and then later you have:

facer face = new facer(...);

Now, in your output, you are trying to find the output facer. Your issue is that you have misunderstood what the Name is on a Swing component.... The name is a value that can be given to a swing component (using setName(String)) to give it some meaningful name for the application.

What you are really looking for is the ClassName, the name of the type of the component.

Your facer class may be in the default package, or it may be in one of your own packages. If, for example, the facer class is in the package com.example.myapp then, if you add the code:

System.out.println("source class name gives me " + source.getClass().getName());

then it will output:

com.example.myapp.facer

If you want just the simple class name, then try:

System.out.println("source class name gives me " + source.getClass().getSimpleName());