Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

setText label for objects

I have an array of objects whose data i trying to display using jlabels

for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
setText(obj[i][j].getName())
setText(obj[i][j].getSchool());
etc...
}
add it to the panel...
add to container..
}
my problem is that the labels are not getting printed. The only thing getting printed is teh last element of the object.
;
0
zizi21
Asked:
zizi21
  • 2
  • 2
1 Solution
 
CEHJCommented:
You need to create a new JLabel for each one and call panel.add on it. If you need to keep the references to each label, you need to use an array of labels too
0
 
CEHJCommented:
Something like

// Outside loop
JLabel[][] labels = new JLabel[i][j];
// The following inside loop
labels[i][j] = new JLabel(obj[i][j].getName());
panel.add(labels[i][j]);
0
 
sciuriwareCommented:
How about:

   JTextArea a = new JTextArea(30, 80);

    for(int i=0;i<3;i++)
    {
          for(int j=0;j<2;j++)
          {
          a.append(obj[i][j].getName() + "   "); // 3 spaces trailing
          a.append(obj[i][j].getSchool() + '\n');  // Newline
    }

If 'a' grows too big, put it in a JScrollPane.

;JOOP!
0
 
sciuriwareCommented:
:))
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now