• Post Reply Bookmark Topic Watch Topic
  • New Topic

ActionListener fail  RSS feed

 
Anders hofel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fellas,

New to the forum and java ^^

Here is my gui class including ActionListener which isn't working.

Besides this class i have a simple person class which can create persons (Name,Title,isRetired)




Warning message at BOLD text: Type safety: The method setListData(Object[]) belongs to the raw type JList. References to generic type JList<E> should be parameterized

Error messages when i click my button:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.toArray(Unknown Source)
at MainFrame$Controller.actionPerformed(MainFrame.java:72)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anders. Welcome to the Ranch!

ArrayStoreException happens when you try to put an object into an array that is the wrong type. Fortunately, the stack trace tells you exactly where this is happening. It's pointing to this line:

personer is an ArrayList<Person>. So in that line you're trying to convert a list of Person to an array of String. Try converting it to an array of the same type.
 
Anders hofel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Hi Anders. Welcome to the Ranch!

ArrayStoreException happens when you try to put an object into an array that is the wrong type. Fortunately, the stack trace tells you exactly where this is happening. It's pointing to this line:

personer is an ArrayList<Person>. So in that line you're trying to convert a list of Person to an array of String. Try converting it to an array of the same type.


lol so obvious

Thanks! and thank you for the welcome.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch (again).
Why are you looking for sources in the actionPerformed method? Create one class per action required, add it to a particular control (e.g. a button) and there is no need for non‑object‑oriented tests for the source.
 
Anders hofel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch (again).
Why are you looking for sources in the actionPerformed method? Create one class per action required, add it to a particular control (e.g. a button) and there is no need for non‑object‑oriented tests for the source.


Hi Campbell, and thank you =)

I did it this way because i will add more buttons later. Please show me how you your solution would look like?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anders hofel wrote:I did it this way because i will add more buttons later. Please show me how you your solution would look like?

The better approach is to have a separate ActionListener for every event, each of which is added only to the control(s) that will fire that action. Then there's no need to check the source, and your action methods are kept nice and simple.

The solution so far would look the same except you'd delete the check, and you'd give the class a more meaningful name (obviously you can't call them all Controller!).
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anders hofel wrote: . . . Please show me how you your solution would look like?
It would look like
a separate ActionListener for every event
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!