Hi everyone,
I'm trying to make a simple application displaying some amount of buttons which has added simmilar but different action. The amount of buttons is not known before compiling because it depends on external data. All the buttons are shown in some container.
How did I get it:
-creating a Panel
-creating an array of buttons
-loading the external data = number of buttons
-creating every button and adding it to Panel using loop
So I have an array of buttons shown in Panel. And it works fine.
Now the actions.
Suppose, that every button has to set its label to "I'm clicked" after clicking.
I can't write a piece of code for every button, because I don't know how many buttons will be required by data and moreover it may be a really great amount.
So I have two potential solutions.
The first one:
int z=0;
while (z<amountOfButtons){
button[z].addActionListener(arrayOfActionListeners[z]);
z++;}
z=0;
while (z<amount){
arrayOfActionListeners[z]=new ActionListener(){
public void actionPerformed(ActionEvent e){
button[z].setLabel("I'm clicked");
}
};
z++;}
The problem is variable z in actionPerformed. NetBeans says "local variable z is accessed from within inner class.".
And the second where the main class implements ActionListener:
int z=0;
while (z<amount){
button[z].addActionListener(this);
z++;}
public void actionPerformed(ActionEvent e) {
int z=0;
while (z < amountOfButtons){
if (e.getSource() == button[z]){
button[z].setLabel("I'm clicked");
}
z++;
}
It gives me it during compiling:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at application.applicationView.actionPerformed(applicationView.java:825)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I won't paste all the source because it's very very long, includes a lot of pieces of code generated by NetBeans and generally all other things work ;)
I'm not good at programming so I'm looking for the easiest soution which I will able to use.