Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Update jlabels in array of jpanels  RSS feed

 
Eamon Williams
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an array of JPanels, each Jpanel is created in a separate class which extends JPanel, and then added to the main jPanel:

jPanelArray[i] = new createJPanel();
jPanelMain.add(jPanelArray[i]);

In createJPanel() a jLabel and jButtons are added with an actionlistener on the button.

My question is, how do I access and update the jLabel component on each jPanel in the array from the main class (where the main Jpanel is added)?

I have a method in the createJPanel class called 'updateLabel(String s)' which I'm trying to call from the main form (where the jPanel array is created) with something like the following:

for (int i = 0; i < 5; i++){
jPanelArray[i].updateLabel(newLabelString);
}

But this does not work (error is java.lang.NullPointerException) partly I think because i'm accessing the jPanelArray[] from a different method to where it was created.

I cannot work out how to access/update the array panel's components in a separate actionPerformed method?

Hope somebody can help out, thanks
 
Eamon Williams
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found a solution for anybody looking up this post (not sure if this is the best way but it seems to work?):

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!