• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java 5 JSlider compile issue

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



This compiles and works properly under 1.5.0, however the compiler throws the error "Track.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details."

and when I do that...

"Track.java:128: warning: [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Hashtable
labelTable.put(new Integer(3), new JLabel("R"));"


As I said, it works, but is there any way to avoid this compiler warning? I know it's pedantic, but it'd be nice to see why it doesn't like it. As far as I see, my example is identical to that on http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html ...


Cheers for your help - you guys rock.
 
Marshal
Posts: 28237
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your Hashtable is always going to have Integers as its keys and JLabels as its data, then you can (and should) declare it as such when you're using Java 5. Like this:And then, when you extract a reference from the Hashtable, you don't have to cast it:This is called "generics" and it's the main new feature in Java 5. If you google for "java generics tutorial" you should find a tutorial about it.
 
Chris Flynn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great stuff - thanks for that
reply
    Bookmark Topic Watch Topic
  • New Topic