CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2022
    Posts
    3

    [RESOLVED] Java Generics : How to pass object having generic type to another class?

    I prepared a code using Java Generics to gain flexibility. I had one class named ImagePanel<T> extends Node<T> class that is also extends Panel (java.awt.Panel) class. The other class is PanelManager manages ImagePanel<T> class objects. I define both classes in the same file (GUI.java file - GUI class).
    However, compiler gives this error :

    "Note: spritesGUI/SpritesWindow.java uses unchecked or unsafe operations.Note: Recompile with -Xlint:unchecked for details."

    The structure is :

    Code:
    class GUI {
    
      class ImagePanel<T> extends Node<T> { ...   }
      class PanelManager { ... public void ImagePanel getSleectedImagePanel(); }
    
    }
    As you see, I have "getSleectedImagePanel()" method which returns ImagePanel. I do not know how to transfer generic type T to PanelManager? Any help is appreciated.

  2. #2
    Join Date
    May 2022
    Posts
    3

    Re: Java Generics : How to pass object having generic type to another class?

    Sorry, "getSleectedImagePanel()" will be "getSelectedImagePanel()" I could not find where to edit my post...

  3. #3
    Join Date
    May 2022
    Posts
    3

    Cool Re: Java Generics : How to pass object having generic type to another class?

    I found the solution :
    I used as it is written :
    class GUI {

    class ImagePanel<T> extends Node<T> { ... }
    class PanelManager { ... public void ImagePanel<T> getSleectedImagePanel(); }

    }

    I also need to declare PanelManager with generic type like that PanelManager<T> and then I replaced all ImagePanel objects with ImagePanel<T>.

    However, I need to compile with "-Xlint:unchecked" option. In other words, for finding unckecked type conversions, I used
    javac -Xlint:unchecked GUI.java

    then it reports all errors/warnings etc. And one by one, I cleared all of them... Solved...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured