-
May 1st, 2022, 09:58 AM
#1
[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.
-
May 1st, 2022, 10:07 AM
#2
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...
-
May 1st, 2022, 10:49 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|