The type is ambiguous. How do I fix it?

edited August 2015 in Library Questions

This code produces the error "The type MouseEvent is ambiguous."

How do I clear up the ambiguity?

import java.awt.event.*;
import g4p_controls.*;

MouseEvent e;

Answers

  • Which MouseEvent do you want? Use its fully qualified name instead of importing it.

    java.awt.event.MouseEvent e;

    This is why wildcard imports are a bad idea. Just import only the classes you actually need.

  • edited August 2015

    I want the g4p_controls mouse event, but when I write:

    import java.awt.event.*;
    import g4p_controls.*;
    
    g4p_controls.MouseEvent e;
    
    I get the error "Cannot find class or type g4p_controls.MouseEvent"
    

    Which leads me to believe that MouseEvent is a class in Processing and the gui builder uses it. So how do I call THAT MouseEvent - I don't want the MouseEvent in the awt.event import. I have to use the wildcard because its the only way I can get itemStateChanged to work without an error complaining about abstract methods.

  • edited August 2015 Answer ✓

    To use Processing's MouseEvent, you don't need to import anything at all, as it is included in Processing's core library. You're getting the error, because you've imported java.awt.event's version of MouseEvent, as well. Remove the Java library import, and everything should work.

    If you still need the java.awt.event import, then @KevinWorkman's advice still applies, in that you will have to explicitly use processing.event.MouseEvent to clear up the ambiguity.


    Edit: I suggested to explicitly use the class, because it was the most fail-safe solution I could give, since I don't know whether or not you may be using Java's MouseEvent in the future, as well.

    But if it is known for a fact that you will not, then it is indeed better to simply import your required events the way KevinWorkman shows below.

  • I need to create an ItemListenr for a CheckboxMenuItem and the wild card is the only way I can get it to work. That blows up MouseEvent which I'm also going to need.

     myCheckboxMenuItem.addItemListener(new ItemListener() {
             public void itemStateChanged(ItemEvent e) {
              CHECK = !CHECK;
    
             }
          });
    }
    
  • Well I need the java.awt.event import because I have a menu with checkbox items. The explicit class you posted works. Thank you.

  • No, you just need to import the ItemListener and ItemEvent directly::

    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
    
Sign In or Register to comment.