1

The issue could be different then what I suspect, but I figure the problem is either I read wrongly when learning about interfaces or it applies differently inside of the action listener.

First off, here is my error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to main.GUI
    at main.GUI$3.actionPerformed(GUI.java:224)
    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$000(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)

The code I am using is as follows:

this.select.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((GUI) e.getSource()).tool = new Selection();
            }
        });

I have an interface called tool, which I make a global variable Tool tool; for the GUI class. Then I attempt in the method above to assign the Selection class to the var tool which implements Tool. Am I doing something wrong and how do I fix it? If you need more code just ask.

Also When I assign a new Selection() to the global var tool outside of the ActionListener it works... so that is what I don't understand.

Zeveso
  • 1,274
  • 3
  • 21
  • 41

2 Answers2

3

Regarding

I thought that java interfaces could hold a subclass, but maybe not in ActionListener… correct?

No, this has nothing to do with your problem. Rather your problem is an incorrect cast pure and simple. The object returned by e.getsource() is not a GUI object but rather an AbstractButton, perhaps a JButton (it's the variable named "select"). The component that has the ActionListener added to it is the one that is returned by ActionEvent#getSource().

Also, I don't see where you have an interface holding a subclass anywhere.

could you do?:

    this.select.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            tool = new Selection(); //??
        }
    });

It's hard to say what you should do as we really don't know what the structure of the rest of your code looks like.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • That makes sense. I have the button inside of a toolbar. Is there anyways I could get out into the GraphicalUserInterface where I have all my global variables? What are my options? I can provide you with the whole class. – Zeveso Jul 08 '12 at 20:33
  • You need references to whatever objects you want to access. Try not to use the term "global variable" around a Java programmer. They really don't exist in the Java world. Perhaps you mean *class fields*. – Hovercraft Full Of Eels Jul 08 '12 at 20:34
  • I suppose you could pastebin your code if it isn't too large. – Hovercraft Full Of Eels Jul 08 '12 at 20:35
  • http://pastebin.com/JHcfJNmS Here is the class, error is on line 224. If you see anything else then let me know of course. Thanks! – Zeveso Jul 09 '12 at 01:38
  • ALSO, note that Tool is an interface and Selection implements Tool. I assume that is a subclass of it. I might be wrong in my terminology, if so... sorry. – Zeveso Jul 09 '12 at 02:01
  • @Zevesco: missing several framework classes, Mapper, Tool, impl.Selection. – Hovercraft Full Of Eels Jul 09 '12 at 02:06
  • @Zeveso: oh, and this line doesn't make sense: `((GUI) e.getSource()).getParent() = new Selection();`. It's not Java. – Hovercraft Full Of Eels Jul 09 '12 at 02:23
  • http://pastebin.com/ftSsMtWk It is java, but is my user defined class. GUI is what I have you before and it worked for one of the other action listeners, but not that one. I think it was because in another tool bar. ANYWAYS, here you go with a new pastie and I am looking into the Action class, but still don't quite understand it... just give me some time... lost power again for a bit so I can only do so much at a time – Zeveso Jul 09 '12 at 17:40
3

I have the button inside of a toolbar.

Use Action, shown here, to encapsulate functionality.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045