1

Okay, so I have this JFrame frame and I'm trying to load all of the components into a JLabel ArrayList. the frame has a LayeredPane and around 20 JLabels scattered across. Some of them have a different z-index/layer index

   ArrayList<JLabel> labelList = new ArrayList<>();
    
    for(Component i : frame.getComponents() ) {
        
        
        if (i instanceof JLabel) {
            labelList.add((JLabel) i);
        }
    }
    
    System.out.println(labelList);

but when I try to print out the ArrayList, it just prints an empty Array.

When I hover over the .getComponents(), it actually shows that all of the JLabels are in fact contained in the method.

And it doesn't give out any errors either, like Component not being able to convert to JLabel etc.

edit: I did as @camickr suggested, and it show that only the contentPane is grabbed. I probably misunderstood how the .getComponents() works.

edit2 just changed the code to:

    ArrayList<JLabel> labelList = new ArrayList<>();
    
    for(Component i : layeredPane.getComponents() ) { //layeredPane being the name of the JLayeredPane, suprisingly

            labelList.add((JLabel) i); 
    }
    
    System.out.println(labelList.toArray().length);

And it works. I am a very dumb indiviudal.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Are the `JLabel` instances *directly* on the `frame` component or are they placed inside other panels, which are added to the `frame` component? The list returned by `getComponents()` is not recursively. – Progman Jan 12 '22 at 17:04
  • See other questions like https://stackoverflow.com/questions/6495769/how-to-get-all-elements-inside-a-jframe – Progman Jan 12 '22 at 17:05
  • Use your tools fully. Set a breakpoint in the debugger at the if statement. Set a display on i so you can inspect the actual type, not what a hover display decides to depict them as. nb: The important thing in play here is learning how dig at problems like this yourself. – Gene Jan 12 '22 at 17:09
  • 1
    Add a `System.out.println(i.getClass())` statement to your code to set what the components are. Components are NOT added directly to the frame. They are added to the content pane of the frame. See: [Using Top Level Containers](https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html) for the basic structure of a frame. If you really need access to the labels why not add them to the ArrayList when you add them to the layered pane? – camickr Jan 12 '22 at 17:09
  • @Progman they are added to the LayeredPane – Jan Jonáš Jan 12 '22 at 17:15
  • @camickr there is around twenty labels that were generated via the eclipse windowbuilder, I know that I could just go to each JLabel declaration and add them to the list there, but I plan on making even more frames with even more JLabels, so I just wanted to see if I could just collect all of them into one place – Jan Jonáš Jan 12 '22 at 17:22
  • 1
    Your last edit you could post it as an answer instead, and removing the insult to yourself, then accept the answer to mark it as solved – Frakcool Jan 12 '22 at 18:33

1 Answers1

0
ArrayList<JLabel> labelList = new ArrayList<>();

for(Component i : layeredPane.getComponents() ) { //layeredPane being the name of the JLayeredPane, suprisingly

        labelList.add((JLabel) i); 
}

System.out.println(labelList.toArray().length);

is the solution. It didn't work due to me trying to grab everything from the frame, but the only thing on the frame was the layered Pane, where all the other components were.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 14 '22 at 07:35