mercredi 21 janvier 2015

Changing JLabel Foreground for multiple labels


Vote count:

0




I am kind of stuck trying to change the foreground colour for some JLabels I have.


I got 42 different items I can activate in my app. Each item has it's own JLabel. I made a boolean array to check if the item is activated or not and another String array to check for the items place in my list.


Now the problem is that each item has it's own JLabel that i want to change the colour of. The JLabels are called char0, char1, ... , char41. I tried to use the place number to get to a specific label but sadly that won't work, so I implemented an array for my labels.



import javax.swing.JLabel;
JLabel[] label = setLabel();
public JLabel[] setLabel(){
JLabel[] labels = {char0,char1,...,char41};
return labels;
}


So basically I got an array of the JLabel type which should hold my 42 labels. Now to get them back and change the colour this is what I tried:



getLabel(place).setForeground(Color.green);
public JLabel getLabel(int place){
return label[place];
}


Sadly I get this exception: "AWT-EventQueue-0" java.lang.NullPointerException at werwolf.GUI.setCharacter(GUI.java:1068) at werwolf.GUI.jButton1MouseClicked(GUI.java:1040) at werwolf.GUI.access$100(GUI.java:17) at werwolf.GUI$2.mouseClicked(GUI.java:199) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6528) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6290) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4881) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) at java.awt.Container.dispatchEventImpl(Container.java:2278) at java.awt.Window.dispatchEventImpl(Window.java:2739) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) at java.awt.EventQueue.access$400(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:697) at java.awt.EventQueue$3.run(EventQueue.java:691) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:719) at java.awt.EventQueue$4.run(EventQueue.java:717) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)


What am I doing wrong? Thanks!!!



asked 1 min ago







Changing JLabel Foreground for multiple labels

Aucun commentaire:

Enregistrer un commentaire