• Post Reply Bookmark Topic Watch Topic
  • New Topic

Jlist & Generics (Moving from Java 6 to 7  RSS feed

 
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Within my Swing project I am trying to extend some standard classes to implement standard look and feel without the need to replicate the setting of properties wherever they are used. To this end in my Java 6 code I had a class like this :-




I am now moving to Java 7 and slowly going through all my code changing the syntax to meet the generics validations etc. I have transformed the code above into this :-



The problem I have is that some references to my code about create a new instance of it using the syntax as follows :-



The problem with my amended Java 7 code is that I'm not calling the original constructor by passing the parameter :-



if I amend the above to call super(items) I get an error "The constructor JList<E>(String[]) is undefined".

Could someone help me untangle myself from this mess please.

Many thanks

Dave
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is just about setting fonts, insets and such for a Swing UI you should use javax.swing.UIDefaults and javax.swing.UIManager. Much easier than overriding each class.

About your generic problem:

You define your own class as generic.

But you use a String array for the data. The constructor of the super class expects the generic type.You could change it to:



Now you are using the generic type. (Oh, and that feature was new to Java 5.)
 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's perfect - it works. I had simplified my code to show only the font's setting, but thanks for the tip anyway. I will eventually get my head around<E> <T> etc but it just looks a bit weird at the moment.

Cheers

Dave
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Talking about changing code to "become more generic", you don't necessarily need Java 7 for this. Since generic is available since Java 5, with Java 6 or even 5 you can make your classes generic.

For such projects, IMHO starts with the version you originally developed from (eg Java 6). Then without any changes run it (no re-compiling) under Java 7 JVM. If everything works as before, compile using Java 7 and move forward from there.
 
David Garratt
Ranch Hand
Posts: 216
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the approach I'm taking. This project which consists of 300+ classes was started with Java 5, migrated to 6 and now to 7. It runs ok in 7 with the exception of a problem determining the printer queue descriptions under OSX, but otherwise ok. I am now going through all the code using eclipse to highlight any deprecated API's and code which does not take advantage of the new Java 7 Generics syntax (as above).

Thanks for your feedback

Dave
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!