Alosyius
Alosyius

Reputation: 9121

Java iterate through JList which contains JPanel - JLabel

I am trying to iterate over a JList where each item contains: JPanel - JLabel

Currently what i have is:

System.out.println("Reading all list items:");
System.out.println("-----------------------");
for (int i = 0; i < menuList.getModel().getSize(); i++) {
     Object item = menuList.getModel().getElementAt(i);;
     System.out.println("Item = " + item);
} 

The output i get is:

Item = javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

Instead i want to access the text that is inside the JPanel.

How could this be done?

Edit: This is how i add my JPanel to the JList

        menuList = new JList(v);
        v = new Vector <String> ();
        menuList.setListData(v);
.....

    // get our images
    Icon pingImage = new javax.swing.ImageIcon(getClass().getResource("/resources/icnNew.png"));

    // add the images to jlabels with text
    JLabel pingLabel = new JLabel("Hi there", pingImage, JLabel.LEFT);

    // create the corresponding panels
    JPanel pingPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

    // add the labels onto the panels
    pingPanel.add(pingLabel);

    v.add(pingPanel);

So the text i want to find is "Hi there"

Upvotes: 0

Views: 2418

Answers (1)

Omar
Omar

Reputation: 699

Then what you need is to check for the elements inside that JPanel. I mean, a panel is a container of UI elements, that said, you need to check for the elements, once you checked for that you need to compare whether it is a label or not, if it is a label then you will be able to get the text of that label.

Can't you show us the code, probably could be easier if you provide the snippet.

Upvotes: 0

Related Questions